{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "963fdca6",
   "metadata": {},
   "source": [
    "# Generating the electronic Hamiltonian\n",
    "\n",
    "For chemistry applications in quantum computing, one needs first to design the electronic Hamiltonian and convert it to qubit Hamiltonian. In this tutorial, we will explain how user can generate the electronic Hamiltonian for their subsequent quantum computing simulation.\n",
    "\n",
    "The electronic Hamiltonian describes the energy of electrons in the electrostatic field of nuclei that are assumed to be stationary with respect to an inertial frame [(1)](https://en.wikipedia.org/wiki/Molecular_Hamiltonian). This operator plays a central role in computational chemistry and physics for calculating properties of molecules including thermal conductivity, specific heat, electrical conductivity, optical properties, magnetic properties, and chemical reactivity.\n",
    "\n",
    "In quantum mechanics generally, the Hamiltonian operator represents the total energy of a system, including both kinetic and potential energy components. The electronic Hamiltonian specifically focuses on the electronic component of this energy within molecular systems.\n",
    "\n",
    "#### Second Quantized formulation.\n",
    "\n",
    "The second quantized formulation expresses the Hamiltonian in terms of creation and annihilation operators that automatically preserve the antisymmetry requirements for fermions. This representation is particularly useful for implementing computational methods in quantum chemistry.\n",
    "\n",
    "$$H = \\sum_{pq} h_{pq} a_p^\\dagger a_q + \\frac{1}{2} \\sum_{pqrs} g_{pqrs} a_p^\\dagger a_q^\\dagger a_r a_s$$\n",
    "\n",
    "where:\n",
    "- $h_{pq}$ are the one-electron integrals\n",
    "- $g_{pqrs}$ are the two-electron integrals\n",
    "- $a_p^\\dagger$ and $a_p$ are the fermion creation and annihilation operators\n",
    "\n",
    "This formulation provides significant advantages for computational implementations and development of electron correlation methods.\n",
    "\n",
    "### Computational Implementation\n",
    "\n",
    "The computational implementation of electronic Hamiltonians involves several key steps that transform the abstract operator into a concrete numerical representation.\n",
    "\n",
    "- Basis Set Expansion: The first step is to express the electronic Hamiltonian in a finite basis of atomic orbitals (AOs). These are typically Gaussian-type orbitals centered on atoms, chosen for computational efficiency\n",
    "- Integral Calculation: Once a basis is chosen, one-electron and two-electron integrals are computed in the AO basis. These represent matrix elements of the Hamiltonian components between different basis functions\n",
    "- Self-Consistent Field Calculation: A self-consistent field (SCF) procedure, typically using the Hartree-Fock method, is performed to obtain molecular orbitals (MOs) as linear combinations of AOs\n",
    "- Integral Transformation: The AO integrals are transformed to the MO basis using the coefficients from the SCF calculation. This transformation is computationally intensive but crucial for correlated methods\n",
    "- Spin Adaptation: For many applications, the spatial MO integrals are converted to spin MO integrals to account for electron spin explicitly\n",
    "\n",
    "In this tutorial, we employ [pyscf](https://github.com/pyscf/pyscf) to perform these calculations of electron integrals."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1c3a83f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pyscf in /usr/local/lib/python3.10/dist-packages (2.10.0)\n",
      "Requirement already satisfied: scipy>=1.6.0 in /usr/local/lib/python3.10/dist-packages (from pyscf) (1.10.1)\n",
      "Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (from pyscf) (59.6.0)\n",
      "Requirement already satisfied: h5py>=2.7 in /usr/local/lib/python3.10/dist-packages (from pyscf) (3.10.0)\n",
      "Requirement already satisfied: numpy!=1.16,!=1.17,>=1.13 in /usr/local/lib/python3.10/dist-packages (from pyscf) (1.26.4)\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
      "\u001b[0mNote: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "# Requires pyscf to be installed\n",
    "%pip install pyscf"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a01426b",
   "metadata": {},
   "source": [
    "To calculate the electronic integrals, and generate the spin molecular Hamiltonian, import the following"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0c795caa",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qchem.classical_pyscf import get_mol_hamiltonian"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95d5146c",
   "metadata": {},
   "source": [
    "To convert from fermionic Hamiltonian to qubit hamiltonian, import the following"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a9df0323",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qchem.hamiltonian import jordan_wigner_fermion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "23ec3969",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import the cudaq module\n",
    "import cudaq\n",
    "\n",
    "# Set the traget\n",
    "# Double precision is recommended for the best performance.\n",
    "cudaq.set_target(\"nvidia\", option = \"fp64\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba002534",
   "metadata": {},
   "source": [
    "## Full space Hamiltonian"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef495ce0",
   "metadata": {},
   "source": [
    "### (a) Generate the molecular Hamiltonian using Restricted Hartree Fock molecular orbitals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1ac10a7e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "output file: qchem/H2-pyscf.log\n",
      "[pyscf] Total number of orbitals =  2\n",
      "[pyscf] Total number of electrons =  2\n",
      "[pyscf] HF energy =  -1.116651247411522\n",
      "[pyscf] Total R-CCSD energy =  -1.1372635051456803\n",
      "-------------------------------\n",
      "Molecular data\n",
      "-------------------------------\n",
      "Number of electrons:  2\n",
      "Total number of spatial orbitals:  2\n",
      "Number of spin molecular orbitals:  4\n",
      "Nuclear repulsion energy:  0.7131768341239892\n",
      "Fermionic hamiltonian:  -1.252101177118797 a_0^† a_0 + -1.252101177118797 b_1^† b_1 + 0.33718713635104136 a_0^† a_0^† a_0 a_0 + 0.33718713635104136 b_1^† b_1^† b_1 b_1 + 0.33718713635104136 a_0^† b_1^† b_1 a_0 + 0.33718713635104136 b_1^† a_0^† a_0 b_1 + 3.469446951953614e-18 a_0^† a_0^† a_0 a_2 + 3.469446951953614e-18 b_1^† b_1^† b_1 b_3 + 3.469446951953614e-18 a_0^† b_1^† b_1 a_2 + 3.469446951953614e-18 b_1^† a_0^† a_0 b_3 + 5.551115123125783e-17 a_0^† a_0^† a_2 a_0 + 5.551115123125783e-17 b_1^† b_1^† b_3 b_1 + 5.551115123125783e-17 a_0^† b_1^† b_3 a_0 + 5.551115123125783e-17 b_1^† a_0^† a_2 b_1 + 0.09066120278229642 a_0^† a_0^† a_2 a_2 + 0.09066120278229642 b_1^† b_1^† b_3 b_3 + 0.09066120278229642 a_0^† b_1^† b_3 a_2 + 0.09066120278229642 b_1^† a_0^† a_2 b_3 + -2.518543302583089e-16 a_0^† a_2 + -2.518543302583089e-16 b_1^† b_3 + 2.7755575615628914e-17 a_0^† a_2^† a_0 a_0 + 2.7755575615628914e-17 b_1^† b_3^† b_1 b_1 + 2.7755575615628914e-17 a_0^† b_3^† b_1 a_0 + 2.7755575615628914e-17 b_1^† a_2^† a_0 b_1 + 0.09066120278229657 a_0^† a_2^† a_0 a_2 + 0.09066120278229657 b_1^† b_3^† b_1 b_3 + 0.09066120278229657 a_0^† b_3^† b_1 a_2 + 0.09066120278229657 b_1^† a_2^† a_0 b_3 + 0.33168193748206176 a_0^† a_2^† a_2 a_0 + 0.33168193748206176 b_1^† b_3^† b_3 b_1 + 0.33168193748206176 a_0^† b_3^† b_3 a_0 + 0.33168193748206176 b_1^† a_2^† a_2 b_1 + 1.249000902703301e-16 a_0^† a_2^† a_2 a_2 + 1.249000902703301e-16 b_1^† b_3^† b_3 b_3 + 1.249000902703301e-16 a_0^† b_3^† b_3 a_2 + 1.249000902703301e-16 b_1^† a_2^† a_2 b_3 + -1.3184012795569792e-18 a_2^† a_0 + -1.3184012795569792e-18 b_3^† b_1 + 3.469446951953614e-17 a_2^† a_0^† a_0 a_0 + 3.469446951953614e-17 b_3^† b_1^† b_1 b_1 + 3.469446951953614e-17 a_2^† b_1^† b_1 a_0 + 3.469446951953614e-17 b_3^† a_0^† a_0 b_1 + 0.331681937482062 a_2^† a_0^† a_0 a_2 + 0.331681937482062 b_3^† b_1^† b_1 b_3 + 0.331681937482062 a_2^† b_1^† b_1 a_2 + 0.331681937482062 b_3^† a_0^† a_0 b_3 + 0.0906612027822965 a_2^† a_0^† a_2 a_0 + 0.0906612027822965 b_3^† b_1^† b_3 b_1 + 0.0906612027822965 a_2^† b_1^† b_3 a_0 + 0.0906612027822965 b_3^† a_0^† a_2 b_1 + 1.1102230246251565e-16 a_2^† a_0^† a_2 a_2 + 1.1102230246251565e-16 b_3^† b_1^† b_3 b_3 + 1.1102230246251565e-16 a_2^† b_1^† b_3 a_2 + 1.1102230246251565e-16 b_3^† a_0^† a_2 b_3 + -0.4763246662830581 a_2^† a_2 + -0.4763246662830581 b_3^† b_3 + 0.09066120278229667 a_2^† a_2^† a_0 a_0 + 0.09066120278229667 b_3^† b_3^† b_1 b_1 + 0.09066120278229667 a_2^† b_3^† b_1 a_0 + 0.09066120278229667 b_3^† a_2^† a_0 b_1 + 5.551115123125783e-17 a_2^† a_2^† a_0 a_2 + 5.551115123125783e-17 b_3^† b_3^† b_1 b_3 + 5.551115123125783e-17 a_2^† b_3^† b_1 a_2 + 5.551115123125783e-17 b_3^† a_2^† a_0 b_3 + 1.6653345369377348e-16 a_2^† a_2^† a_2 a_0 + 1.6653345369377348e-16 b_3^† b_3^† b_3 b_1 + 1.6653345369377348e-16 a_2^† b_3^† b_3 a_0 + 1.6653345369377348e-16 b_3^† a_2^† a_2 b_1 + 0.348641686688851 a_2^† a_2^† a_2 a_2 + 0.348641686688851 b_3^† b_3^† b_3 b_3 + 0.348641686688851 a_2^† b_3^† b_3 a_2 + 0.348641686688851 b_3^† a_2^† a_2 b_3\n",
      "Energies:  {'hf': -1.116651247411522, 'mp2': None, 'casci': None, 'casscf': None, 'ccsd': -1.1372635051456803, 'fci': None, 'nuclear_repulsion': 0.7131768341239892}\n",
      "-------------------------------\n",
      "Jordan-Wigner transformation\n",
      "-------------------------------\n",
      "[Cudaq] Qubit hamiltonian:  (-0.0996319+0i) + (0.171106+0i) * Z0 + (0.171106+0i) * Z1 + (-0.22251+0i) * Z2 + (-0.22251+0i) * Z3 + (0.168594+0i) * Z0Z1 + (0.12051+0i) * Z0Z2 + (0.165841+0i) * Z0Z3 + (0.165841+0i) * Z1Z2 + (0.12051+0i) * Z1Z3 + (0.174321+0i) * Z2Z3 + (-0.0453306+0i) * X0X1Y2Y3 + (0.0453306+0i) * X0Y1Y2X3 + (0.0453306+0i) * Y0X1X2Y3 + (-0.0453306+0i) * Y0Y1X2X3\n",
      "[Cudaq] Number of terms in the Qubit hamiltonian:  15\n"
     ]
    }
   ],
   "source": [
    "H2_mol = \"qchem/H2.xyz\"\n",
    "\n",
    "# Run HF, ccsd and compute the spin molecular hamiltonian using the HF molecular orbitals.\n",
    "molecular_data = get_mol_hamiltonian(xyz=H2_mol, spin=0, charge=0, basis='sto3g', ccsd=True, verbose=True)\n",
    "\n",
    "obi = molecular_data[0]\n",
    "tbi = molecular_data[1]\n",
    "e_nn = molecular_data[2]\n",
    "nelectrons = molecular_data[3]\n",
    "norbitals = molecular_data[4]\n",
    "fermionic_hamiltonian = molecular_data[5]\n",
    "energies = molecular_data[6]\n",
    "\n",
    "print('-------------------------------')\n",
    "print('Molecular data')\n",
    "print('-------------------------------')\n",
    "print('Number of electrons: ', nelectrons)\n",
    "print('Total number of spatial orbitals: ', norbitals)\n",
    "print('Number of spin molecular orbitals: ', 2 * norbitals)\n",
    "print('Nuclear repulsion energy: ', e_nn)\n",
    "print('Fermionic hamiltonian: ', fermionic_hamiltonian)\n",
    "print('Energies: ', energies)\n",
    "\n",
    "print('-------------------------------')\n",
    "print('Jordan-Wigner transformation')\n",
    "print('-------------------------------')\n",
    "# Compute the qubit hamiltonian.\n",
    "spin_ham = jordan_wigner_fermion(obi, tbi, e_nn, tolerance = 1e-12)\n",
    "\n",
    "print('[Cudaq] Qubit hamiltonian: ', spin_ham)\n",
    "print('[Cudaq] Number of terms in the Qubit hamiltonian: ', spin_ham.term_count)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95f11228",
   "metadata": {},
   "source": [
    "### (b) Generate the molecular Hamiltonian using Unrestricted Hartree Fock molecular orbitals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "30c5c17d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "overwrite output file: qchem/H2-pyscf.log\n",
      "[pyscf] Total number of alpha molecular orbitals =  2\n",
      "[pyscf] Total number of beta molecular orbitals =  2\n",
      "[pyscf] Total number of electrons =  2\n",
      "[pyscf] HF energy =  -1.1166512474115222\n",
      "[pyscf] Total UR-CCSD energy =  -1.1372635051456819\n",
      "-------------------------------\n",
      "Molecular data\n",
      "-------------------------------\n",
      "Number of electrons:  2\n",
      "Total number of spatial orbitals:  2\n",
      "Number of spin molecular orbitals:  4\n",
      "Nuclear repulsion energy:  0.7131768341239892\n",
      "Fermionic hamiltonian:  -1.252101177118797 a_0^† a_0 + -1.252101177118797 b_1^† b_1 + -4.738989351833402e-16 a_0^† a_2 + -4.738989351833402e-16 b_1^† b_3 + -1.9852838903630502e-16 a_2^† a_0 + -1.9852838903630502e-16 b_3^† b_1 + -0.47632466628305814 a_2^† a_2 + -0.47632466628305814 b_3^† b_3 + 0.3371871363510414 a_0^† a_0^† a_0 a_0 + 0.3371871363510414 b_1^† b_1^† b_1 b_1 + 0.3371871363510414 a_0^† b_1^† b_1 a_0 + 0.3371871363510414 b_1^† a_0^† a_0 b_1 + 5.551115123125783e-17 a_0^† a_0^† a_2 a_0 + 5.551115123125783e-17 b_1^† b_1^† b_3 b_1 + 5.551115123125783e-17 a_0^† b_1^† b_3 a_0 + 5.551115123125783e-17 b_1^† a_0^† a_2 b_1 + 1.734723475976807e-17 a_0^† a_0^† a_0 a_2 + 1.734723475976807e-17 b_1^† b_1^† b_1 b_3 + 1.734723475976807e-17 a_0^† b_1^† b_1 a_2 + 1.734723475976807e-17 b_1^† a_0^† a_0 b_3 + 0.09066120278229645 a_0^† a_0^† a_2 a_2 + 0.09066120278229645 b_1^† b_1^† b_3 b_3 + 0.09066120278229645 a_0^† b_1^† b_3 a_2 + 0.09066120278229645 b_1^† a_0^† a_2 b_3 + 0.0 a_0^† a_2^† a_0 a_0 + 0.0 b_1^† b_3^† b_1 b_1 + 0.0 a_0^† b_3^† b_1 a_0 + 0.0 b_1^† a_2^† a_0 b_1 + 0.331681937482062 a_0^† a_2^† a_2 a_0 + 0.331681937482062 b_1^† b_3^† b_3 b_1 + 0.331681937482062 a_0^† b_3^† b_3 a_0 + 0.331681937482062 b_1^† a_2^† a_2 b_1 + 0.09066120278229658 a_0^† a_2^† a_0 a_2 + 0.09066120278229658 b_1^† b_3^† b_1 b_3 + 0.09066120278229658 a_0^† b_3^† b_1 a_2 + 0.09066120278229658 b_1^† a_2^† a_0 b_3 + -1.3877787807814457e-17 a_0^† a_2^† a_2 a_2 + -1.3877787807814457e-17 b_1^† b_3^† b_3 b_3 + -1.3877787807814457e-17 a_0^† b_3^† b_3 a_2 + -1.3877787807814457e-17 b_1^† a_2^† a_2 b_3 + 5.551115123125783e-17 a_2^† a_0^† a_0 a_0 + 5.551115123125783e-17 b_3^† b_1^† b_1 b_1 + 5.551115123125783e-17 a_2^† b_1^† b_1 a_0 + 5.551115123125783e-17 b_3^† a_0^† a_0 b_1 + 0.09066120278229653 a_2^† a_0^† a_2 a_0 + 0.09066120278229653 b_3^† b_1^† b_3 b_1 + 0.09066120278229653 a_2^† b_1^† b_3 a_0 + 0.09066120278229653 b_3^† a_0^† a_2 b_1 + 0.33168193748206204 a_2^† a_0^† a_0 a_2 + 0.33168193748206204 b_3^† b_1^† b_1 b_3 + 0.33168193748206204 a_2^† b_1^† b_1 a_2 + 0.33168193748206204 b_3^† a_0^† a_0 b_3 + 5.551115123125783e-17 a_2^† a_0^† a_2 a_2 + 5.551115123125783e-17 b_3^† b_1^† b_3 b_3 + 5.551115123125783e-17 a_2^† b_1^† b_3 a_2 + 5.551115123125783e-17 b_3^† a_0^† a_2 b_3 + 0.09066120278229668 a_2^† a_2^† a_0 a_0 + 0.09066120278229668 b_3^† b_3^† b_1 b_1 + 0.09066120278229668 a_2^† b_3^† b_1 a_0 + 0.09066120278229668 b_3^† a_2^† a_0 b_1 + 2.0816681711721685e-16 a_2^† a_2^† a_2 a_0 + 2.0816681711721685e-16 b_3^† b_3^† b_3 b_1 + 2.0816681711721685e-16 a_2^† b_3^† b_3 a_0 + 2.0816681711721685e-16 b_3^† a_2^† a_2 b_1 + 1.1102230246251565e-16 a_2^† a_2^† a_0 a_2 + 1.1102230246251565e-16 b_3^† b_3^† b_1 b_3 + 1.1102230246251565e-16 a_2^† b_3^† b_1 a_2 + 1.1102230246251565e-16 b_3^† a_2^† a_0 b_3 + 0.3486416866888511 a_2^† a_2^† a_2 a_2 + 0.3486416866888511 b_3^† b_3^† b_3 b_3 + 0.3486416866888511 a_2^† b_3^† b_3 a_2 + 0.3486416866888511 b_3^† a_2^† a_2 b_3\n",
      "Energies:  {'hf': -1.1166512474115222, 'mp2': None, 'casci': None, 'casscf': None, 'ccsd': -1.1372635051456819, 'fci': None, 'nuclear_repulsion': 0.7131768341239892}\n",
      "-------------------------------\n",
      "Jordan-Wigner transformation\n",
      "-------------------------------\n",
      "[Cudaq] Qubit hamiltonian:  (-0.0996319+0i) + (0.171106+0i) * Z0 + (0.171106+0i) * Z1 + (-0.22251+0i) * Z2 + (-0.22251+0i) * Z3 + (0.168594+0i) * Z0Z1 + (0.12051+0i) * Z0Z2 + (0.165841+0i) * Z0Z3 + (0.165841+0i) * Z1Z2 + (0.12051+0i) * Z1Z3 + (0.174321+0i) * Z2Z3 + (-0.0453306+0i) * X0X1Y2Y3 + (0.0453306+0i) * X0Y1Y2X3 + (0.0453306+0i) * Y0X1X2Y3 + (-0.0453306+0i) * Y0Y1X2X3\n",
      "[Cudaq] Number of terms in the Qubit hamiltonian:  15\n"
     ]
    }
   ],
   "source": [
    "H2_mol = \"qchem/H2.xyz\"\n",
    "\n",
    "# Run HF, ccsd and compute the spin molecular hamiltonian using the HF molecular orbitals.\n",
    "molecular_data = get_mol_hamiltonian(xyz=H2_mol, spin=0, charge=0, basis='sto3g', UR=True, ccsd=True, verbose=True)\n",
    "\n",
    "obi = molecular_data[0]\n",
    "tbi = molecular_data[1]\n",
    "e_nn = molecular_data[2]\n",
    "nelectrons = molecular_data[3]\n",
    "norbitals = molecular_data[4]\n",
    "fermionic_hamiltonian = molecular_data[5]\n",
    "energies = molecular_data[6]\n",
    "\n",
    "print('-------------------------------')\n",
    "print('Molecular data')\n",
    "print('-------------------------------')\n",
    "print('Number of electrons: ', nelectrons)\n",
    "print('Total number of spatial orbitals: ', norbitals)\n",
    "print('Number of spin molecular orbitals: ', 2 * norbitals)\n",
    "print('Nuclear repulsion energy: ', e_nn)\n",
    "print('Fermionic hamiltonian: ', fermionic_hamiltonian)\n",
    "print('Energies: ', energies)\n",
    "\n",
    "print('-------------------------------')\n",
    "print('Jordan-Wigner transformation')\n",
    "print('-------------------------------')\n",
    "# Compute the qubit hamiltonian.\n",
    "spin_ham = jordan_wigner_fermion(obi, tbi, e_nn, tolerance = 1e-12)\n",
    "\n",
    "print('[Cudaq] Qubit hamiltonian: ', spin_ham)\n",
    "print('[Cudaq] Number of terms in the Qubit hamiltonian: ', spin_ham.term_count)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f04721d",
   "metadata": {},
   "source": [
    "## Active space Hamiltonian:\n",
    "\n",
    "The active space Hamiltonian [(2)](https://en.wikipedia.org/wiki/Complete_active_space) is a reduced-dimensionality quantum chemical model that focuses computational resources on the most chemically relevant orbitals and electrons. This approach enables practical calculations for systems with strong electron correlation by partitioning orbitals into three classes:\n",
    "\n",
    "- Core orbitals: Fully occupied and treated as inert\n",
    "- Active orbitals: Partially occupied, explicitly correlated\n",
    "- Virtual orbitals: Unoccupied and excluded\n",
    "\n",
    "The active space Hamiltonian $H^{AS}$ derives from the full electronic Hamiltonian through orbital space partitioning:\n",
    "\n",
    "$$H^{AS} = \\sum_{pq}^{\\text{active}} \\tilde{h}_{pq} \\hat{a}_p^{\\dagger} \\hat{a}_q + \\frac{1}{2} \\sum_{pqrs}^{\\text{active}} g_{pqrs} \\hat{a}_p^{\\dagger} \\hat{a}_q^{\\dagger} \\hat{a}_r \\hat{a}_s + E_{\\text{core}}$$\n",
    "\n",
    "Where:\n",
    "- $\\tilde{h}_{pq}$ are modified one-electron integrals incorporating mean-field effects from core orbitals\n",
    "- $g_{pqrs}$ are two-electron integrals within the active space\n",
    "- $E_{\\text{core}}$ accounts for frozen core contributions\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d6ef665",
   "metadata": {},
   "source": [
    "### (a) Generate the active space hamiltonian using RHF molecular orbitals."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "de8309cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "output file: qchem/H2O-pyscf.log\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[pyscf] Total number of orbitals =  13\n",
      "[pyscf] Total number of electrons =  10\n",
      "[pyscf] HF energy =  -75.98397553727895\n",
      "[pyscf] R-CASCI energy using molecular orbitals=  -75.98508981028928\n",
      "[pyscf] R-CCSD energy of the active space using molecular orbitals=  -75.98508980454683\n",
      "-------------------------------\n",
      "Molecular data\n",
      "-------------------------------\n",
      "Number of electrons in the active space:  4\n",
      "Number of spatial orbitals in the active space:  4\n",
      "Number of spin molecular orbitals:  8\n",
      "Core energy:  -69.78937499839189\n",
      "CASCI Energy:  -75.98508981028928\n",
      "-------------------------------\n",
      "Jordan-Wigner transformation\n",
      "-------------------------------\n",
      "[Cudaq] Qubit hamiltonian:  (-74.3886+0i) + (0.429698+0i) * Z0 + (0.429698+0i) * Z1 + (0.395076+0i) * Z2 + (0.395076+0i) * Z3 + (0.0246717+0i) * Z4 + (0.0246717+0i) * Z5 + (-0.0124873+0i) * Z6 + (-0.0124873+0i) * Z7 + (0.183927+0i) * Z0Z1 + (0.154956+0i) * Z0Z2 + (0.1664+0i) * Z0Z3 + (0.0441992+0i) * X0Z1Z2Z3X4 + (0.0441992+0i) * Y0Z1Z2Z3Y4 + (0.0835026+0i) * Z0Z4 + (0.0902279+0i) * Z0Z5 + (0.0881962+0i) * Z0Z6 + (0.0939854+0i) * Z0Z7 + (0.1664+0i) * Z1Z2 + (0.154956+0i) * Z1Z3 + (0.0902279+0i) * Z1Z4 + (0.0441992+0i) * X1Z2Z3Z4X5 + (0.0441992+0i) * Y1Z2Z3Z4Y5 + (0.0835026+0i) * Z1Z5 + (0.0939854+0i) * Z1Z6 + (0.0881962+0i) * Z1Z7 + (0.190993+0i) * Z2Z3 + (0.089279+0i) * Z2Z4 + (0.0924599+0i) * Z2Z5 + (0.0920865+0i) * Z2Z6 + (0.0941389+0i) * Z2Z7 + (0.0924599+0i) * Z3Z4 + (0.089279+0i) * Z3Z5 + (0.0941389+0i) * Z3Z6 + (0.0920865+0i) * Z3Z7 + (0.0843783+0i) * Z4Z5 + (0.061204+0i) * Z4Z6 + (0.0833701+0i) * Z4Z7 + (0.0833701+0i) * Z5Z6 + (0.061204+0i) * Z5Z7 + (0.0864277+0i) * Z6Z7 + (0.0184753+0i) * Z0X1Z2Z3Z4X5 + (0.0184753+0i) * Z0Y1Z2Z3Z4Y5 + (0.0184753+0i) * X0Z2Z3X4 + (0.0184753+0i) * Y0Z2Z3Y4 + (-0.0114445+0i) * X0X1Y2Y3 + (0.0114445+0i) * X0Y1Y2X3 + (0.0114445+0i) * Y0X1X2Y3 + (-0.0114445+0i) * Y0Y1X2X3 + (-0.00672531+0i) * X0X1Y4Y5 + (0.00672531+0i) * X0Y1Y4X5 + (0.00672531+0i) * Y0X1X4Y5 + (-0.00672531+0i) * Y0Y1X4X5 + (-0.00578919+0i) * X0X1Y6Y7 + (0.00578919+0i) * X0Y1Y6X7 + (0.00578919+0i) * Y0X1X6Y7 + (-0.00578919+0i) * Y0Y1X6X7 + (0.01658+0i) * X0Z1Z3X4 + (0.01658+0i) * Y0Z1Z3Y4 + (-8.79249e-05+0i) * X0Z1X2X3Z4X5 + (-8.79249e-05+0i) * X0Z1X2Y3Z4Y5 + (-8.79249e-05+0i) * Y0Z1Y2X3Z4X5 + (-8.79249e-05+0i) * Y0Z1Y2Y3Z4Y5 + (0.0166679+0i) * X0Z1Z2X4 + (0.0166679+0i) * Y0Z1Z2Y4 + (0.000423558+0i) * X0Z1Z2Z3X4Z5 + (0.000423558+0i) * Y0Z1Z2Z3Y4Z5 + (-0.00705531+0i) * X0Z1Z2Z3Z4X5Y6Y7 + (0.00705531+0i) * X0Z1Z2Z3Z4Y5Y6X7 + (0.00705531+0i) * Y0Z1Z2Z3Z4X5X6Y7 + (-0.00705531+0i) * Y0Z1Z2Z3Z4Y5X6X7 + (0.00707794+0i) * X0Z1Z2Z3X4Z6 + (0.00707794+0i) * Y0Z1Z2Z3Y4Z6 + (2.26249e-05+0i) * X0Z1Z2Z3X4Z7 + (2.26249e-05+0i) * Y0Z1Z2Z3Y4Z7 + (0.0166679+0i) * X1Z3Z4X5 + (0.0166679+0i) * Y1Z3Z4Y5 + (8.79249e-05+0i) * X1X2Y3Y4 + (-8.79249e-05+0i) * X1Y2Y3X4 + (-8.79249e-05+0i) * Y1X2X3Y4 + (8.79249e-05+0i) * Y1Y2X3X4 + (0.01658+0i) * X1Z2Z4X5 + (0.01658+0i) * Y1Z2Z4Y5 + (0.000423558+0i) * X1Z2Z3X5 + (0.000423558+0i) * Y1Z2Z3Y5 + (-0.00705531+0i) * X1Z2Z3X4X6X7 + (-0.00705531+0i) * X1Z2Z3Y4Y6X7 + (-0.00705531+0i) * Y1Z2Z3X4X6Y7 + (-0.00705531+0i) * Y1Z2Z3Y4Y6Y7 + (2.26249e-05+0i) * X1Z2Z3Z4X5Z6 + (2.26249e-05+0i) * Y1Z2Z3Z4Y5Z6 + (0.00707794+0i) * X1Z2Z3Z4X5Z7 + (0.00707794+0i) * Y1Z2Z3Z4Y5Z7 + (-0.00318093+0i) * X2X3Y4Y5 + (0.00318093+0i) * X2Y3Y4X5 + (0.00318093+0i) * Y2X3X4Y5 + (-0.00318093+0i) * Y2Y3X4X5 + (-0.00205243+0i) * X2X3Y6Y7 + (0.00205243+0i) * X2Y3Y6X7 + (0.00205243+0i) * Y2X3X6Y7 + (-0.00205243+0i) * Y2Y3X6X7 + (-0.0221661+0i) * X4X5Y6Y7 + (0.0221661+0i) * X4Y5Y6X7 + (0.0221661+0i) * Y4X5X6Y7 + (-0.0221661+0i) * Y4Y5X6X7\n",
      "[Cudaq] Number of terms in the Qubit hamiltonian:  105\n"
     ]
    }
   ],
   "source": [
    "H2O_mol = \"qchem/H2O.xyz\"\n",
    "\n",
    "# Run HF, ccsd, casci and compute the active space spin molecular hamiltonian\n",
    "# using the HF molecular orbitals with 4 electron and 4 orbitals in the active space.\n",
    "molecular_data = get_mol_hamiltonian(xyz=H2O_mol, spin=0, charge=0, basis='631g', nele_cas=4, norb_cas=4, \n",
    "                                     ccsd=True, casci=True, verbose=True)\n",
    "\n",
    "obi = molecular_data[0]\n",
    "tbi = molecular_data[1]\n",
    "ecore = molecular_data[2]\n",
    "nelectrons = molecular_data[3]\n",
    "norbitals = molecular_data[4]\n",
    "fermionic_hamiltonian = molecular_data[5]\n",
    "energies = molecular_data[6]\n",
    "\n",
    "print('-------------------------------')\n",
    "print('Molecular data')\n",
    "print('-------------------------------')\n",
    "print('Number of electrons in the active space: ', nelectrons)\n",
    "print('Number of spatial orbitals in the active space: ', norbitals)\n",
    "print('Number of spin molecular orbitals: ', 2 * norbitals)\n",
    "print('Core energy: ', ecore)\n",
    "print('CASCI Energy: ', energies['casci'])\n",
    "#print('Fermionic hamiltonian: ', fermionic_hamiltonian)\n",
    "\n",
    "print('-------------------------------')\n",
    "print('Jordan-Wigner transformation')\n",
    "print('-------------------------------')\n",
    "# Compute the qubit hamiltonian.\n",
    "spin_ham = jordan_wigner_fermion(obi, tbi, ecore)\n",
    "print('[Cudaq] Qubit hamiltonian: ', spin_ham)\n",
    "print('[Cudaq] Number of terms in the Qubit hamiltonian: ', spin_ham.term_count)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b382bd22",
   "metadata": {},
   "source": [
    "### (b) Generate the active space Hamiltonian using the natural orbitals computed from MP2 simulation "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2a8cdb09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "overwrite output file: qchem/H2O-pyscf.log\n",
      "[pyscf] Total number of orbitals =  13\n",
      "[pyscf] Total number of electrons =  10\n",
      "[pyscf] HF energy =  -75.9839755372789\n",
      "[pyscf] R-MP2 energy=  -76.11280670065037\n",
      "[pyscf] Natural orbital occupation number from R-MP2: \n",
      "[1.99995663e+00 1.99012740e+00 1.98197049e+00 1.97722983e+00\n",
      " 1.97515174e+00 2.23585706e-02 2.09527832e-02 1.73621504e-02\n",
      " 1.00838241e-02 2.32351484e-03 1.74875097e-03 4.27482714e-04\n",
      " 3.06831462e-04]\n",
      "[pyscf] R-CASCI energy using natural orbitals=  -76.03304212600104\n",
      "[pyscf] R-CCSD energy of the active space using natural orbitals=  -76.03293564805286\n",
      "-------------------------------\n",
      "Molecular data\n",
      "-------------------------------\n",
      "Number of electrons in the active space:  4\n",
      "Number of spatial orbitals in the active space:  4\n",
      "Number of spin molecular orbitals:  8\n",
      "Core energy:  -69.59209064078001\n",
      "-------------------------------\n",
      "Jordan-Wigner transformation\n",
      "-------------------------------\n",
      "[Cudaq] Qubit hamiltonian:  (-73.4465+0i) + (0.19855+0i) * Z0 + (0.19855+0i) * Z1 + (0.233578+0i) * Z2 + (0.233578+0i) * Z3 + (-0.284531+0i) * Z4 + (-0.284531+0i) * Z5 + (-0.317727+0i) * Z6 + (-0.317727+0i) * Z7 + (0.169845+0i) * Z0Z1 + (0.137855+0i) * Z0Z2 + (0.153384+0i) * Z0Z3 + (0.145992+0i) * Z0Z4 + (0.156607+0i) * Z0Z5 + (-0.018065+0i) * X0Z1Z2Z3Z4Z5X6 + (-0.018065+0i) * Y0Z1Z2Z3Z4Z5Y6 + (0.123378+0i) * Z0Z6 + (0.164004+0i) * Z0Z7 + (0.153384+0i) * Z1Z2 + (0.137855+0i) * Z1Z3 + (0.156607+0i) * Z1Z4 + (0.145992+0i) * Z1Z5 + (0.164004+0i) * Z1Z6 + (-0.018065+0i) * X1Z2Z3Z4Z5Z6X7 + (-0.018065+0i) * Y1Z2Z3Z4Z5Z6Y7 + (0.123378+0i) * Z1Z7 + (0.163223+0i) * Z2Z3 + (0.00966883+0i) * X2Z3X4 + (0.00966883+0i) * Y2Z3Y4 + (0.119937+0i) * Z2Z4 + (0.163499+0i) * Z2Z5 + (0.140568+0i) * Z2Z6 + (0.151859+0i) * Z2Z7 + (0.163499+0i) * Z3Z4 + (0.00966883+0i) * X3Z4X5 + (0.00966883+0i) * Y3Z4Y5 + (0.119937+0i) * Z3Z5 + (0.151859+0i) * Z3Z6 + (0.140568+0i) * Z3Z7 + (0.175043+0i) * Z4Z5 + (0.141038+0i) * Z4Z6 + (0.159822+0i) * Z4Z7 + (0.159822+0i) * Z5Z6 + (0.141038+0i) * Z5Z7 + (0.170651+0i) * Z6Z7 + (0.042603+0i) * Z0X1Z2Z3Z4Z5Z6X7 + (0.042603+0i) * Z0Y1Z2Z3Z4Z5Z6Y7 + (0.042603+0i) * X0Z2Z3Z4Z5X6 + (0.042603+0i) * Y0Z2Z3Z4Z5Y6 + (-0.0155289+0i) * X0X1Y2Y3 + (0.0155289+0i) * X0Y1Y2X3 + (0.0155289+0i) * Y0X1X2Y3 + (-0.0155289+0i) * Y0Y1X2X3 + (0.00424669+0i) * X0X1Y2Z3Z4Y5 + (-0.00424669+0i) * X0Y1Y2Z3Z4X5 + (-0.00424669+0i) * Y0X1X2Z3Z4Y5 + (0.00424669+0i) * Y0Y1X2Z3Z4X5 + (0.00424669+0i) * X0X1X3X4 + (0.00424669+0i) * X0Y1Y3X4 + (0.00424669+0i) * Y0X1X3Y4 + (0.00424669+0i) * Y0Y1Y3Y4 + (-0.0106147+0i) * X0X1Y4Y5 + (0.0106147+0i) * X0Y1Y4X5 + (0.0106147+0i) * Y0X1X4Y5 + (-0.0106147+0i) * Y0Y1X4X5 + (-0.0406258+0i) * X0X1Y6Y7 + (0.0406258+0i) * X0Y1Y6X7 + (0.0406258+0i) * Y0X1X6Y7 + (-0.0406258+0i) * Y0Y1X6X7 + (-0.0382825+0i) * Z0X2Z3X4 + (-0.0382825+0i) * Z0Y2Z3Y4 + (0.0355881+0i) * X0Z1Z3Z4Z5X6 + (0.0355881+0i) * Y0Z1Z3Z4Z5Y6 + (0.0184957+0i) * X0Z1X2X4Z5X6 + (-0.00516964+0i) * X0Z1X2Y4Z5Y6 + (0.0236653+0i) * X0Z1Y2Y4Z5X6 + (0.0236653+0i) * Y0Z1X2X4Z5Y6 + (-0.00516964+0i) * Y0Z1Y2X4Z5X6 + (0.0184957+0i) * Y0Z1Y2Y4Z5Y6 + (-0.0340358+0i) * Z0X3Z4X5 + (-0.0340358+0i) * Z0Y3Z4Y5 + (0.00538795+0i) * X0Z1X2X3Z4Z5Z6X7 + (0.00538795+0i) * X0Z1X2Y3Z4Z5Z6Y7 + (0.00538795+0i) * Y0Z1Y2X3Z4Z5Z6X7 + (0.00538795+0i) * Y0Z1Y2Y3Z4Z5Z6Y7 + (0.0302001+0i) * X0Z1Z2Z4Z5X6 + (0.0302001+0i) * Y0Z1Z2Z4Z5Y6 + (0.0102326+0i) * X0Z1Z2X3Y4Z5Z6Y7 + (-0.0102326+0i) * X0Z1Z2Y3Y4Z5Z6X7 + (-0.0102326+0i) * Y0Z1Z2X3X4Z5Z6Y7 + (0.0102326+0i) * Y0Z1Z2Y3X4Z5Z6X7 + (0.0338979+0i) * X0Z1Z2X3X5X6 + (0.0338979+0i) * X0Z1Z2Y3Y5X6 + (0.0338979+0i) * Y0Z1Z2X3X5Y6 + (0.0338979+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(0.0184957+0i) * X1Z2X3X5Z6X7 + (-0.00516964+0i) * X1Z2X3Y5Z6Y7 + (0.0236653+0i) * X1Z2Y3Y5Z6X7 + (0.0236653+0i) * Y1Z2X3X5Z6Y7 + (-0.00516964+0i) * Y1Z2Y3X5Z6X7 + (0.0184957+0i) * Y1Z2Y3Y5Z6Y7 + (-0.0154022+0i) * X1Z2X3X4Z5X6 + (-0.0154022+0i) * X1Z2X3Y4Z5Y6 + (-0.0154022+0i) * Y1Z2Y3X4Z5X6 + (-0.0154022+0i) * Y1Z2Y3Y4Z5Y6 + (0.0379243+0i) * X1Z2Z3Z5Z6X7 + (0.0379243+0i) * Y1Z2Z3Z5Z6Y7 + (-0.00600788+0i) * X1Z2Z3X4Y5Y6 + (0.00600788+0i) * X1Z2Z3Y4Y5X6 + (0.00600788+0i) * Y1Z2Z3X4X5Y6 + (-0.00600788+0i) * Y1Z2Z3Y4X5X6 + (0.0439322+0i) * X1Z2Z3Z4Z6X7 + (0.0439322+0i) * Y1Z2Z3Z4Z6Y7 + (0.0445998+0i) * X1Z2Z3Z4Z5X7 + (0.0445998+0i) * Y1Z2Z3Z4Z5Y7 + (-0.0327198+0i) * Z2X3Z4X5 + (-0.0327198+0i) * Z2Y3Z4Y5 + (-0.0327198+0i) * X2X4 + (-0.0327198+0i) * Y2Y4 + (-0.0435622+0i) * X2X3Y4Y5 + (0.0435622+0i) * X2Y3Y4X5 + (0.0435622+0i) * Y2X3X4Y5 + (-0.0435622+0i) * Y2Y3X4X5 + (-0.0112912+0i) * X2X3Y6Y7 + (0.0112912+0i) * X2Y3Y6X7 + (0.0112912+0i) * Y2X3X6Y7 + (-0.0112912+0i) * Y2Y3X6X7 + (-0.0425308+0i) * X2Z3X4Z5 + (-0.0425308+0i) * Y2Z3Y4Z5 + (0.00684156+0i) * X2Z3Z4X5Y6Y7 + (-0.00684156+0i) * X2Z3Z4Y5Y6X7 + (-0.00684156+0i) * Y2Z3Z4X5X6Y7 + (0.00684156+0i) * Y2Z3Z4Y5X6X7 + (-0.0395088+0i) * X2Z3X4Z6 + (-0.0395088+0i) * Y2Z3Y4Z6 + (-0.0326672+0i) * X2Z3X4Z7 + (-0.0326672+0i) * Y2Z3Y4Z7 + (-0.0425308+0i) * X3X5 + (-0.0425308+0i) * Y3Y5 + (0.00684156+0i) * X3X4X6X7 + (0.00684156+0i) * X3Y4Y6X7 + (0.00684156+0i) * Y3X4X6Y7 + (0.00684156+0i) * Y3Y4Y6Y7 + (-0.0326672+0i) * X3Z4X5Z6 + (-0.0326672+0i) * Y3Z4Y5Z6 + (-0.0395088+0i) * X3Z4X5Z7 + (-0.0395088+0i) * Y3Z4Y5Z7 + (-0.0187841+0i) * X4X5Y6Y7 + (0.0187841+0i) * X4Y5Y6X7 + (0.0187841+0i) * Y4X5X6Y7 + (-0.0187841+0i) * Y4Y5X6X7\n",
      "[Cudaq] Number of terms in the Qubit hamiltonian:  185\n"
     ]
    }
   ],
   "source": [
    "H2O_mol = \"qchem/H2O.xyz\"\n",
    "\n",
    "# Run HF, MP2, ccsd, casci and compute the active space spin molecular hamiltonian.\n",
    "# Inspect the active space from the natural orbitals. Natural orbitals are computed using MP2. \n",
    "# The active space is defined by 4 electrons and 4 orbitals.\n",
    "# The natural orbitals are used to compute the electron integrals.\n",
    "molecular_data = get_mol_hamiltonian(xyz=H2O_mol, spin=0, charge=0, basis='631g',nele_cas=4, norb_cas=4, \n",
    "                                     MP2=True, natorb=True, ccsd=True, casci=True, integrals_natorb=True, verbose=True)\n",
    "\n",
    "obi = molecular_data[0]\n",
    "tbi = molecular_data[1]\n",
    "ecore = molecular_data[2]\n",
    "nelectrons = molecular_data[3]\n",
    "norbitals = molecular_data[4]\n",
    "fermionic_hamiltonian = molecular_data[5]\n",
    "energies = molecular_data[6]\n",
    "\n",
    "print('-------------------------------')\n",
    "print('Molecular data')\n",
    "print('-------------------------------')\n",
    "print('Number of electrons in the active space: ', nelectrons)\n",
    "print('Number of spatial orbitals in the active space: ', norbitals)\n",
    "print('Number of spin molecular orbitals: ', 2 * norbitals)\n",
    "print('Core energy: ', ecore)\n",
    "#print('Fermionic hamiltonian: ', fermionic_hamiltonian)\n",
    "\n",
    "print('-------------------------------')\n",
    "print('Jordan-Wigner transformation')\n",
    "print('-------------------------------')\n",
    "# Compute the qubit hamiltonian.\n",
    "spin_ham = jordan_wigner_fermion(obi, tbi, ecore)\n",
    "print('[Cudaq] Qubit hamiltonian: ', spin_ham)\n",
    "print('[Cudaq] Number of terms in the Qubit hamiltonian: ', spin_ham.term_count)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa3ce84f",
   "metadata": {},
   "source": [
    "### (c) Generate the active space Hamiltonian computed from the CASSCF molecular orbitals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d2728947",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "overwrite output file: qchem/H2O-pyscf.log\n",
      "[pyscf] Total number of orbitals =  13\n",
      "[pyscf] Total number of electrons =  10\n",
      "[pyscf] HF energy =  -75.98397553727898\n",
      "[pyscf] R-MP2 energy=  -76.11280670065045\n",
      "[pyscf] Natural orbital occupation number from R-MP2: \n",
      "[1.99995663e+00 1.99012740e+00 1.98197049e+00 1.97722983e+00\n",
      " 1.97515174e+00 2.23585706e-02 2.09527832e-02 1.73621504e-02\n",
      " 1.00838241e-02 2.32351484e-03 1.74875097e-03 4.27482714e-04\n",
      " 3.06831462e-04]\n",
      "[pyscf] R-CASSCF energy using natural orbitals=  -76.03702384069334\n",
      "-------------------------------\n",
      "Molecular data\n",
      "-------------------------------\n",
      "Number of electrons in the active space:  4\n",
      "Number of spatial orbitals in the active space:  4\n",
      "Number of spin molecular orbitals:  8\n",
      "Core energy:  -69.14810818513212\n",
      "-------------------------------\n",
      "Jordan-Wigner transformation\n",
      "-------------------------------\n",
      "[Cudaq] Qubit hamiltonian:  (-73.4246+0i) + (0.286+0i) * Z0 + (0.286+0i) * Z1 + (0.232955+0i) * Z2 + (0.232955+0i) * Z3 + (-0.262533+0i) * Z4 + (-0.262533+0i) * Z5 + (-0.24543+0i) * Z6 + (-0.24543+0i) * Z7 + (0.181709+0i) * Z0Z1 + (9.44339e-10+0i) * X0Z1X2 + (9.44339e-10+0i) * Y0Z1Y2 + (0.135721+0i) * Z0Z2 + (0.158846+0i) * Z0Z3 + (7.97384e-11+0i) * X0Z1Z2Z3X4 + (7.97384e-11+0i) * Y0Z1Z2Z3Y4 + (0.147141+0i) * Z0Z4 + (0.159353+0i) * Z0Z5 + (-0.00801359+0i) * X0Z1Z2Z3Z4Z5X6 + (-0.00801359+0i) * Y0Z1Z2Z3Z4Z5Y6 + (0.130427+0i) * Z0Z6 + (0.173887+0i) * Z0Z7 + (0.158846+0i) * Z1Z2 + (9.44339e-10+0i) * X1Z2X3 + (9.44339e-10+0i) * Y1Z2Y3 + (0.135721+0i) * Z1Z3 + (0.159353+0i) * Z1Z4 + (7.97384e-11+0i) * X1Z2Z3Z4X5 + (7.97384e-11+0i) * Y1Z2Z3Z4Y5 + (0.147141+0i) * Z1Z5 + (0.173887+0i) * Z1Z6 + (-0.00801359+0i) * X1Z2Z3Z4Z5Z6X7 + (-0.00801359+0i) * Y1Z2Z3Z4Z5Z6Y7 + (0.130427+0i) * Z1Z7 + (0.163669+0i) * Z2Z3 + (-0.005669+0i) * X2Z3X4 + (-0.005669+0i) * Y2Z3Y4 + (0.119773+0i) * Z2Z4 + (0.163692+0i) * Z2Z5 + (-1.83834e-11+0i) * X2Z3Z4Z5X6 + (-1.83834e-11+0i) * Y2Z3Z4Z5Y6 + (0.139625+0i) * Z2Z6 + (0.155968+0i) * Z2Z7 + (0.163692+0i) * Z3Z4 + (-0.005669+0i) * X3Z4X5 + (-0.005669+0i) * Y3Z4Y5 + (0.119773+0i) * Z3Z5 + (0.155968+0i) * Z3Z6 + (-1.83834e-11+0i) * X3Z4Z5Z6X7 + (-1.83834e-11+0i) * Y3Z4Z5Z6Y7 + (0.139625+0i) * Z3Z7 + (0.173287+0i) * Z4Z5 + (3.20355e-10+0i) * X4Z5X6 + (3.20355e-10+0i) * Y4Z5Y6 + (0.133367+0i) * Z4Z6 + (0.16168+0i) * Z4Z7 + (0.16168+0i) * Z5Z6 + (3.20355e-10+0i) * X5Z6X7 + (3.20355e-10+0i) * Y5Z6Y7 + (0.133367+0i) * Z5Z7 + (0.176899+0i) * Z6Z7 + (7.2938e-11+0i) * Z0X1Z2X3 + (7.2938e-11+0i) * Z0Y1Z2Y3 + (-2.48794e-11+0i) * Z0X1Z2Z3Z4X5 + (-2.48794e-11+0i) * Z0Y1Z2Z3Z4Y5 + (0.0405626+0i) * Z0X1Z2Z3Z4Z5Z6X7 + (0.0405626+0i) * Z0Y1Z2Z3Z4Z5Z6Y7 + (7.2938e-11+0i) * X0X2 + (7.2938e-11+0i) * Y0Y2 + (-2.48794e-11+0i) * X0Z2Z3X4 + (-2.48794e-11+0i) * Y0Z2Z3Y4 + (0.0405626+0i) * X0Z2Z3Z4Z5X6 + (0.0405626+0i) * Y0Z2Z3Z4Z5Y6 + (-0.0231244+0i) * X0X1Y2Y3 + (0.0231244+0i) * X0Y1Y2X3 + (0.0231244+0i) * Y0X1X2Y3 + (-0.0231244+0i) * Y0Y1X2X3 + (-0.00729609+0i) * X0X1Y2Z3Z4Y5 + (0.00729609+0i) * X0Y1Y2Z3Z4X5 + (0.00729609+0i) * Y0X1X2Z3Z4Y5 + (-0.00729609+0i) * Y0Y1X2Z3Z4X5 + (2.27614e-12+0i) * X0X1Y2Z3Z4Z5Z6Y7 + (-2.27614e-12+0i) * X0Y1Y2Z3Z4Z5Z6X7 + (-2.27614e-12+0i) * Y0X1X2Z3Z4Z5Z6Y7 + (2.27614e-12+0i) * Y0Y1X2Z3Z4Z5Z6X7 + (-0.00729609+0i) * X0X1X3X4 + (-0.00729609+0i) * X0Y1Y3X4 + (-0.00729609+0i) * Y0X1X3Y4 + (-0.00729609+0i) * Y0Y1Y3Y4 + (2.27614e-12+0i) * X0X1X3Z4Z5X6 + (2.27614e-12+0i) * X0Y1Y3Z4Z5X6 + (2.27614e-12+0i) * Y0X1X3Z4Z5Y6 + (2.27614e-12+0i) * Y0Y1Y3Z4Z5Y6 + (-0.0122114+0i) * X0X1Y4Y5 + (0.0122114+0i) * X0Y1Y4X5 + (0.0122114+0i) * Y0X1X4Y5 + (-0.0122114+0i) * Y0Y1X4X5 + (1.62381e-11+0i) * X0X1Y4Z5Z6Y7 + (-1.62381e-11+0i) * X0Y1Y4Z5Z6X7 + (-1.62381e-11+0i) * Y0X1X4Z5Z6Y7 + (1.62381e-11+0i) * Y0Y1X4Z5Z6X7 + (1.62381e-11+0i) * X0X1X5X6 + (1.62381e-11+0i) * X0Y1Y5X6 + (1.62381e-11+0i) * Y0X1X5Y6 + (1.62381e-11+0i) * Y0Y1Y5Y6 + (-0.0434604+0i) * X0X1Y6Y7 + (0.0434604+0i) * X0Y1Y6X7 + (0.0434604+0i) * Y0X1X6Y7 + (-0.0434604+0i) * Y0Y1X6X7 + (0.0337551+0i) * Z0X2Z3X4 + (0.0337551+0i) * Z0Y2Z3Y4 + (-1.54976e-11+0i) * Z0X2Z3Z4Z5X6 + (-1.54976e-11+0i) * Z0Y2Z3Z4Z5Y6 + (-1.90296e-12+0i) * X0Z1Z3X4 + (-1.90296e-12+0i) * Y0Z1Z3Y4 + (0.0362638+0i) * X0Z1Z3Z4Z5X6 + (0.0362638+0i) * Y0Z1Z3Z4Z5Y6 + (-0.0108497+0i) * X0Z1X2X4Z5X6 + (0.00966188+0i) * X0Z1X2Y4Z5Y6 + (-0.0205116+0i) * X0Z1Y2Y4Z5X6 + (-0.0205116+0i) * Y0Z1X2X4Z5Y6 + (0.00966188+0i) * Y0Z1Y2X4Z5X6 + (-0.0108497+0i) * Y0Z1Y2Y4Z5Y6 + (0.026459+0i) * Z0X3Z4X5 + (0.026459+0i) * Z0Y3Z4Y5 + (-1.32215e-11+0i) * Z0X3Z4Z5Z6X7 + (-1.32215e-11+0i) * Z0Y3Z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z3Z4Z5Y6Z7 + (0.026459+0i) * Z1X2Z3X4 + (0.026459+0i) * Z1Y2Z3Y4 + (-1.32215e-11+0i) * Z1X2Z3Z4Z5X6 + (-1.32215e-11+0i) * Z1Y2Z3Z4Z5Y6 + (1.48061e-10+0i) * X1X3 + (1.48061e-10+0i) * Y1Y3 + (1.72934e-12+0i) * X1Z3Z4X5 + (1.72934e-12+0i) * Y1Z3Z4Y5 + (0.0269701+0i) * X1Z3Z4Z5Z6X7 + (0.0269701+0i) * Y1Z3Z4Z5Z6Y7 + (3.63229e-12+0i) * X1X2Y3Y4 + (-3.63229e-12+0i) * X1Y2Y3X4 + (-3.63229e-12+0i) * Y1X2X3Y4 + (3.63229e-12+0i) * Y1Y2X3X4 + (-0.0092937+0i) * X1X2Y3Z4Z5Y6 + (0.0092937+0i) * X1Y2Y3Z4Z5X6 + (0.0092937+0i) * Y1X2X3Z4Z5Y6 + (-0.0092937+0i) * Y1Y2X3Z4Z5X6 + (1.28862e-11+0i) * X1X2X4X5 + (1.28862e-11+0i) * X1Y2Y4X5 + (1.28862e-11+0i) * Y1X2X4Y5 + (1.28862e-11+0i) * Y1Y2Y4Y5 + (-0.0342907+0i) * X1X2X4Z5Z6X7 + (-0.0342907+0i) * X1Y2Y4Z5Z6X7 + (-0.0342907+0i) * Y1X2X4Z5Z6Y7 + (-0.0342907+0i) * Y1Y2Y4Z5Z6Y7 + (-0.0137791+0i) * X1X2Y5Y6 + (0.0137791+0i) * X1Y2Y5X6 + (0.0137791+0i) * Y1X2X5Y6 + (-0.0137791+0i) * Y1Y2X5X6 + (2.21699e-11+0i) * X1X2X6X7 + (2.21699e-11+0i) * X1Y2Y6X7 + (2.21699e-11+0i) * Y1X2X6Y7 + (2.21699e-11+0i) * Y1Y2Y6Y7 + (0.0337551+0i) * Z1X3Z4X5 + (0.0337551+0i) * Z1Y3Z4Y5 + (-1.54976e-11+0i) * Z1X3Z4Z5Z6X7 + (-1.54976e-11+0i) * Z1Y3Z4Z5Z6Y7 + (-1.90296e-12+0i) * X1Z2Z4X5 + (-1.90296e-12+0i) * Y1Z2Z4Y5 + (0.0362638+0i) * X1Z2Z4Z5Z6X7 + (0.0362638+0i) * Y1Z2Z4Z5Z6Y7 + (-0.0108497+0i) * X1Z2X3X5Z6X7 + (0.00966188+0i) * X1Z2X3Y5Z6Y7 + (-0.0205116+0i) * X1Z2Y3Y5Z6X7 + (-0.0205116+0i) * Y1Z2X3X5Z6Y7 + (0.00966188+0i) * Y1Z2Y3X5Z6X7 + (-0.0108497+0i) * Y1Z2Y3Y5Z6Y7 + (-1.76605e-11+0i) * Z1X4Z5X6 + (-1.76605e-11+0i) * Z1Y4Z5Y6 + (1.89864e-12+0i) * X1Z2X3Z4 + (1.89864e-12+0i) * Y1Z2Y3Z4 + (0.023441+0i) * X1Z2X3X4Z5X6 + (0.023441+0i) * X1Z2X3Y4Z5Y6 + (0.023441+0i) * Y1Z2Y3X4Z5X6 + (0.023441+0i) * Y1Z2Y3Y4Z5Y6 + (-2.95981e-11+0i) * X1Z2Z3X5 + (-2.95981e-11+0i) * Y1Z2Z3Y5 + (0.0301078+0i) * X1Z2Z3Z5Z6X7 + (0.0301078+0i) * Y1Z2Z3Z5Z6Y7 + (-0.010417+0i) * X1Z2Z3X4Y5Y6 + (0.010417+0i) * X1Z2Z3Y4Y5X6 + (0.010417+0i) * Y1Z2Z3X4X5Y6 + (-0.010417+0i) * Y1Z2Z3Y4X5X6 + (2.30462e-11+0i) * X1Z2Z3X4X6X7 + (2.30462e-11+0i) * X1Z2Z3Y4Y6X7 + (2.30462e-11+0i) * Y1Z2Z3X4X6Y7 + (2.30462e-11+0i) * Y1Z2Z3Y4Y6Y7 + (-3.38986e-11+0i) * Z1X5Z6X7 + (-3.38986e-11+0i) * Z1Y5Z6Y7 + (-1.09876e-11+0i) * X1Z2X3Z5 + (-1.09876e-11+0i) * Y1Z2Y3Z5 + (0.0405248+0i) * X1Z2Z3Z4Z6X7 + (0.0405248+0i) * Y1Z2Z3Z4Z6Y7 + (6.51428e-11+0i) * X1Z2X3Z6 + (6.51428e-11+0i) * Y1Z2Y3Z6 + (-2.0878e-11+0i) * X1Z2Z3Z4X5Z6 + (-2.0878e-11+0i) * Y1Z2Z3Z4Y5Z6 + (0.0379837+0i) * X1Z2Z3Z4Z5X7 + (0.0379837+0i) * Y1Z2Z3Z4Z5Y7 + (4.29729e-11+0i) * X1Z2X3Z7 + (4.29729e-11+0i) * Y1Z2Y3Z7 + (-4.39242e-11+0i) * X1Z2Z3Z4X5Z7 + (-4.39242e-11+0i) * Y1Z2Z3Z4Y5Z7 + (0.0306081+0i) * Z2X3Z4X5 + (0.0306081+0i) * Z2Y3Z4Y5 + (2.62864e-11+0i) * Z2X3Z4Z5Z6X7 + (2.62864e-11+0i) * Z2Y3Z4Z5Z6Y7 + (0.0306081+0i) * X2X4 + (0.0306081+0i) * Y2Y4 + (2.62864e-11+0i) * X2Z4Z5X6 + (2.62864e-11+0i) * Y2Z4Z5Y6 + (-0.043919+0i) * X2X3Y4Y5 + (0.043919+0i) * X2Y3Y4X5 + (0.043919+0i) * Y2X3X4Y5 + (-0.043919+0i) * Y2Y3X4X5 + (-2.31362e-12+0i) * X2X3Y4Z5Z6Y7 + (2.31362e-12+0i) * X2Y3Y4Z5Z6X7 + (2.31362e-12+0i) * Y2X3X4Z5Z6Y7 + (-2.31362e-12+0i) * Y2Y3X4Z5Z6X7 + (-2.31362e-12+0i) * X2X3X5X6 + (-2.31362e-12+0i) * X2Y3Y5X6 + (-2.31362e-12+0i) * Y2X3X5Y6 + (-2.31362e-12+0i) * Y2Y3Y5Y6 + (-0.0163427+0i) * X2X3Y6Y7 + (0.0163427+0i) * X2Y3Y6X7 + (0.0163427+0i) * Y2X3X6Y7 + (-0.0163427+0i) * Y2Y3X6X7 + (8.2426e-11+0i) * Z2X4Z5X6 + (8.2426e-11+0i) * Z2Y4Z5Y6 + (2.04748e-11+0i) * X2Z3Z5X6 + (2.04748e-11+0i) * Y2Z3Z5Y6 + (8.01124e-11+0i) * Z2X5Z6X7 + (8.01124e-11+0i) * Z2Y5Z6Y7 + (0.0360254+0i) * X2Z3X4Z5 + (0.0360254+0i) * Y2Z3Y4Z5 + (1.10555e-11+0i) * X2Z3X4X5Z6X7 + (1.10555e-11+0i) * X2Z3X4Y5Z6Y7 + (1.10555e-11+0i) * Y2Z3Y4X5Z6X7 + (1.10555e-11+0i) * Y2Z3Y4Y5Z6Y7 + (9.4193e-12+0i) * X2Z3Z4X6 + (9.4193e-12+0i) * Y2Z3Z4Y6 + (-0.013094+0i) * X2Z3Z4X5Y6Y7 + (0.013094+0i) * X2Z3Z4Y5Y6X7 + (0.013094+0i) * Y2Z3Z4X5X6Y7 + (-0.013094+0i) * Y2Z3Z4Y5X6X7 + (0.0358977+0i) * X2Z3X4Z6 + (0.0358977+0i) * Y2Z3Y4Z6 + (0.0228038+0i) * X2Z3X4Z7 + (0.0228038+0i) * Y2Z3Y4Z7 + (-2.22627e-11+0i) * X2Z3Z4Z5X6Z7 + (-2.22627e-11+0i) * Y2Z3Z4Z5Y6Z7 + (8.01124e-11+0i) * Z3X4Z5X6 + (8.01124e-11+0i) * Z3Y4Z5Y6 + (0.0360254+0i) * X3X5 + (0.0360254+0i) * Y3Y5 + (9.4193e-12+0i) * X3Z5Z6X7 + (9.4193e-12+0i) * Y3Z5Z6Y7 + (-1.10555e-11+0i) * X3X4Y5Y6 + (1.10555e-11+0i) * X3Y4Y5X6 + (1.10555e-11+0i) * Y3X4X5Y6 + (-1.10555e-11+0i) * Y3Y4X5X6 + (-0.013094+0i) * X3X4X6X7 + (-0.013094+0i) * X3Y4Y6X7 + (-0.013094+0i) * Y3X4X6Y7 + (-0.013094+0i) * Y3Y4Y6Y7 + (8.2426e-11+0i) * Z3X5Z6X7 + (8.2426e-11+0i) * Z3Y5Z6Y7 + (2.04748e-11+0i) * X3Z4Z6X7 + (2.04748e-11+0i) * Y3Z4Z6Y7 + (0.0228038+0i) * X3Z4X5Z6 + (0.0228038+0i) * Y3Z4Y5Z6 + (-2.22627e-11+0i) * X3Z4Z5X7 + (-2.22627e-11+0i) * Y3Z4Z5Y7 + (0.0358977+0i) * X3Z4X5Z7 + (0.0358977+0i) * Y3Z4Y5Z7 + (-4.37108e-11+0i) * Z4X5Z6X7 + (-4.37108e-11+0i) * Z4Y5Z6Y7 + (-4.37108e-11+0i) * X4X6 + (-4.37108e-11+0i) * Y4Y6 + (-0.0283132+0i) * X4X5Y6Y7 + (0.0283132+0i) * X4Y5Y6X7 + (0.0283132+0i) * Y4X5X6Y7 + (-0.0283132+0i) * Y4Y5X6X7 + (-3.28131e-11+0i) * X4Z5X6Z7 + (-3.28131e-11+0i) * Y4Z5Y6Z7 + (-3.28131e-11+0i) * X5X7 + (-3.28131e-11+0i) * Y5Y7\n",
      "[Cudaq] Number of terms in the Qubit hamiltonian:  361\n"
     ]
    }
   ],
   "source": [
    "H2O_mol = \"qchem/H2O.xyz\"\n",
    "\n",
    "# Run HF, MP2, casscf and compute the active space spin molecular hamiltonian.\n",
    "# Inspect and the active space from the natural orbitals. Natural orbitals are computed using MP2. \n",
    "# The natural orbitals are used with casscf calaculations.\n",
    "# The casscf  orbitals are used to compute the electron integrals.\n",
    "\n",
    "molecular_data = get_mol_hamiltonian(xyz=H2O_mol, spin=0, charge=0, basis='631g', nele_cas=4, norb_cas=4, \n",
    "                                     MP2=True, natorb=True, casscf=True, integrals_casscf=True, verbose=True)\n",
    "\n",
    "# If do not want to use the natural orbitals to inspect active space, set natorb=False (default).\n",
    "#molecular_data = get_mol_hamiltonian(xyz=H2O_mol, spin=0, charge=0, basis='631g', nele_cas=4, norb_cas=4, \n",
    "#                                    casscf=True, integrals_casscf=True, verbose=True)\n",
    "\n",
    "obi = molecular_data[0]\n",
    "tbi = molecular_data[1]\n",
    "ecore = molecular_data[2]\n",
    "nelectrons = molecular_data[3]\n",
    "norbitals = molecular_data[4]\n",
    "fermionic_hamiltonian = molecular_data[5]\n",
    "energies = molecular_data[6]\n",
    "\n",
    "print('-------------------------------')\n",
    "print('Molecular data')\n",
    "print('-------------------------------')\n",
    "print('Number of electrons in the active space: ', nelectrons)\n",
    "print('Number of spatial orbitals in the active space: ', norbitals)\n",
    "print('Number of spin molecular orbitals: ', 2 * norbitals)\n",
    "print('Core energy: ', ecore)\n",
    "#print('Fermionic hamiltonian: ', fermionic_hamiltonian)\n",
    "\n",
    "print('-------------------------------')\n",
    "print('Jordan-Wigner transformation')\n",
    "print('-------------------------------')\n",
    "# Compute the qubit hamiltonian.\n",
    "spin_ham = jordan_wigner_fermion(obi, tbi, ecore)\n",
    "print('[Cudaq] Qubit hamiltonian: ', spin_ham)\n",
    "print('[Cudaq] Number of terms in the Qubit hamiltonian: ', spin_ham.term_count)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f8dac70",
   "metadata": {},
   "source": [
    "### (d) Generate the electronic Hamiltonian using ROHF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9b051f99",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "output file: N 0-pyscf.log\n",
      "[pyscf] Total number of orbitals =  18\n",
      "[pyscf] Total number of electrons =  13\n",
      "[pyscf] HF energy =  -108.28029717676156\n",
      "[pyscf] R-CASCI energy using molecular orbitals=  -108.37916901674112\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARN: RCCSD method does not support ROHF method. ROHF object is converted to UHF object and UCCSD method is called.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[pyscf] R-CCSD energy of the active space using molecular orbitals=  -108.37763087821682\n",
      "-------------------------------\n",
      "Molecular data\n",
      "-------------------------------\n",
      "Number of electrons in the active space:  5\n",
      "Number of spatial orbitals in the active space:  6\n",
      "Number of spin molecular orbitals:  12\n",
      "Core energy:  -97.73731859734099\n",
      "[Cudaq] Qubit hamiltonian:  (-106.644+0i) + (0.261983+0i) * Z0 + (0.261983+0i) * Z1 + (0.261983+0i) * Z2 + (0.261983+0i) * Z3 + (0.237545+0i) * Z4 + (0.237545+0i) * Z5 + (0.0601943+0i) * Z6 + (0.0601943+0i) * Z7 + (0.0601943+0i) * Z8 + (0.0601943+0i) * Z9 + (-0.199869+0i) * Z10 + (-0.199869+0i) * Z11 + (0.144151+0i) * Z0Z1 + (0.127483+0i) * Z0Z2 + (0.133039+0i) * Z0Z3 + (0.123982+0i) * Z0Z4 + (0.131017+0i) * Z0Z5 + (0.0931373+0i) * Z0Z6 + (0.134985+0i) * Z0Z7 + (0.120593+0i) * Z0Z8 + (0.125611+0i) * Z0Z9 + (0.0907631+0i) * Z0Z10 + (0.0949805+0i) * Z0Z11 + (0.133039+0i) * Z1Z2 + (0.127483+0i) * Z1Z3 + (0.131017+0i) * Z1Z4 + (0.123982+0i) * Z1Z5 + (0.134985+0i) * Z1Z6 + (0.0931373+0i) * Z1Z7 + (0.125611+0i) * Z1Z8 + (0.120593+0i) * Z1Z9 + (0.0949805+0i) * Z1Z10 + (0.0907631+0i) * Z1Z11 + (0.144151+0i) * Z2Z3 + (0.123982+0i) * Z2Z4 + (0.131017+0i) * Z2Z5 + (0.120593+0i) * Z2Z6 + (0.125611+0i) * Z2Z7 + (0.0931373+0i) * Z2Z8 + (0.134985+0i) * Z2Z9 + (0.0907631+0i) * Z2Z10 + (0.0949805+0i) * Z2Z11 + (0.131017+0i) * Z3Z4 + (0.123982+0i) * Z3Z5 + (0.125611+0i) * Z3Z6 + (0.120593+0i) * Z3Z7 + (0.134985+0i) * Z3Z8 + (0.0931373+0i) * Z3Z9 + (0.0949805+0i) * Z3Z10 + (0.0907631+0i) * Z3Z11 + (0.148494+0i) * Z4Z5 + (0.119366+0i) * Z4Z6 + (0.128149+0i) * Z4Z7 + (0.119366+0i) * Z4Z8 + (0.128149+0i) * Z4Z9 + (0.0924767+0i) * Z4Z10 + (0.0997354+0i) * Z4Z11 + (0.128149+0i) * Z5Z6 + (0.119366+0i) * Z5Z7 + (0.128149+0i) * Z5Z8 + (0.119366+0i) * Z5Z9 + (0.0997354+0i) * Z5Z10 + (0.0924767+0i) * Z5Z11 + (0.130646+0i) * Z6Z7 + (0.115745+0i) * Z6Z8 + (0.120712+0i) * Z6Z9 + (0.0880899+0i) * Z6Z10 + (0.0932951+0i) * Z6Z11 + (0.120712+0i) * Z7Z8 + (0.115745+0i) * Z7Z9 + (0.0932951+0i) * Z7Z10 + (0.0880899+0i) * Z7Z11 + (0.130646+0i) * Z8Z9 + (0.0880899+0i) * Z8Z10 + (0.0932951+0i) * Z8Z11 + (0.0932951+0i) * Z9Z10 + (0.0880899+0i) * Z9Z11 + (0.0905739+0i) * Z10Z11 + (-0.00555603+0i) * X0X1Y2Y3 + (0.00555603+0i) * X0Y1Y2X3 + (0.00555603+0i) * Y0X1X2Y3 + (-0.00555603+0i) * Y0Y1X2X3 + (-0.00703472+0i) * X0X1Y4Y5 + (0.00703472+0i) * X0Y1Y4X5 + (0.00703472+0i) * Y0X1X4Y5 + (-0.00703472+0i) * Y0Y1X4X5 + (-0.0418473+0i) * X0X1Y6Y7 + (0.0418473+0i) * X0Y1Y6X7 + (0.0418473+0i) * Y0X1X6Y7 + (-0.0418473+0i) * Y0Y1X6X7 + (-0.00594236+0i) * X0X1Y6Z7Z8Y9 + (0.00594236+0i) * X0Y1Y6Z7Z8X9 + (0.00594236+0i) * Y0X1X6Z7Z8Y9 + (-0.00594236+0i) * Y0Y1X6Z7Z8X9 + (-0.00594236+0i) * X0X1X7X8 + (-0.00594236+0i) * X0Y1Y7X8 + (-0.00594236+0i) * Y0X1X7Y8 + (-0.00594236+0i) * Y0Y1Y7Y8 + (-0.00501858+0i) * X0X1Y8Y9 + (0.00501858+0i) * X0Y1Y8X9 + (0.00501858+0i) * Y0X1X8Y9 + (-0.00501858+0i) * Y0Y1X8X9 + (-0.00421737+0i) * X0X1Y10Y11 + (0.00421737+0i) * X0Y1Y10X11 + (0.00421737+0i) * Y0X1X10Y11 + (-0.00421737+0i) * Y0Y1X10X11 + (0.00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+0i) * Y0Z1Y2X7Z8X9 + (0.0046866+0i) * Y0Z1Y2Y7Z8Y9 + (-0.00122537+0i) * X0Z1Z2Z3X4X7Z8Z9Z10X11 + (-0.00122537+0i) * X0Z1Z2Z3X4Y7Z8Z9Z10Y11 + (-0.00122537+0i) * Y0Z1Z2Z3Y4X7Z8Z9Z10X11 + (-0.00122537+0i) * Y0Z1Z2Z3Y4Y7Z8Z9Z10Y11 + (0.00442998+0i) * X0Z1X2Z8 + (0.00442998+0i) * Y0Z1Y2Z8 + (-0.00151238+0i) * X0Z1X2Z9 + (-0.00151238+0i) * Y0Z1Y2Z9 + (-0.00019282+0i) * X0Z1Z2Z3X4X9Z10X11 + (-0.00019282+0i) * X0Z1Z2Z3X4Y9Z10Y11 + (-0.00019282+0i) * Y0Z1Z2Z3Y4X9Z10X11 + (-0.00019282+0i) * Y0Z1Z2Z3Y4Y9Z10Y11 + (0.00594236+0i) * X1X2X6X7 + (0.00594236+0i) * X1Y2Y6X7 + (0.00594236+0i) * Y1X2X6Y7 + (0.00594236+0i) * Y1Y2Y6Y7 + (-0.00314844+0i) * X1X2X6Z7Z8X9 + (-0.00314844+0i) * X1Y2Y6Z7Z8X9 + (-0.00314844+0i) * Y1X2X6Z7Z8Y9 + (-0.00314844+0i) * Y1Y2Y6Z7Z8Y9 + (-0.0336802+0i) * X1X2Y7Y8 + (0.0336802+0i) * X1Y2Y7X8 + (0.0336802+0i) * Y1X2X7Y8 + (-0.0336802+0i) * Y1Y2X7X8 + (-0.00594236+0i) * X1X2X8X9 + (-0.00594236+0i) * X1Y2Y8X9 + (-0.00594236+0i) * Y1X2X8Y9 + (-0.00594236+0i) * Y1Y2Y8Y9 + (0.00153816+0i) * X1Z2X3X7Z8X9 + (-0.0289936+0i) * X1Z2X3Y7Z8Y9 + (0.0305318+0i) * X1Z2Y3Y7Z8X9 + (0.0305318+0i) * Y1Z2X3X7Z8Y9 + (-0.0289936+0i) * Y1Z2Y3X7Z8X9 + (0.00153816+0i) * Y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3Z4Y5Y9Z10Y11 + (-0.00151238+0i) * Z1X6Z7X8 + (-0.00151238+0i) * Z1Y6Z7Y8 + (0.00151238+0i) * X1Z2X3Z6 + (0.00151238+0i) * Y1Z2Y3Z6 + (0.0046866+0i) * X1Z2X3X6Z7X8 + (0.0046866+0i) * X1Z2X3Y6Z7Y8 + (0.0046866+0i) * Y1Z2Y3X6Z7X8 + (0.0046866+0i) * Y1Z2Y3Y6Z7Y8 + (-0.00122537+0i) * X1Z2Z3Z4X5X6Z7Z8Z9X10 + (-0.00122537+0i) * X1Z2Z3Z4X5Y6Z7Z8Z9Y10 + (-0.00122537+0i) * Y1Z2Z3Z4Y5X6Z7Z8Z9X10 + (-0.00122537+0i) * Y1Z2Z3Z4Y5Y6Z7Z8Z9Y10 + (0.00442998+0i) * Z1X7Z8X9 + (0.00442998+0i) * Z1Y7Z8Y9 + (-0.00442998+0i) * X1Z2X3Z7 + (-0.00442998+0i) * Y1Z2Y3Z7 + (-0.00151238+0i) * X1Z2X3Z8 + (-0.00151238+0i) * Y1Z2Y3Z8 + (-0.00019282+0i) * X1Z2Z3Z4X5X8Z9X10 + (-0.00019282+0i) * X1Z2Z3Z4X5Y8Z9Y10 + (-0.00019282+0i) * Y1Z2Z3Z4Y5X8Z9X10 + (-0.00019282+0i) * Y1Z2Z3Z4Y5Y8Z9Y10 + (0.00442998+0i) * X1Z2X3Z9 + (0.00442998+0i) * Y1Z2Y3Z9 + (-0.00703472+0i) * X2X3Y4Y5 + (0.00703472+0i) * X2Y3Y4X5 + (0.00703472+0i) * Y2X3X4Y5 + (-0.00703472+0i) * Y2Y3X4X5 + (-0.00501858+0i) * X2X3Y6Y7 + (0.00501858+0i) * X2Y3Y6X7 + (0.00501858+0i) * Y2X3X6Y7 + (-0.00501858+0i) * Y2Y3X6X7 + (0.00594236+0i) * X2X3Y6Z7Z8Y9 + (-0.00594236+0i) * X2Y3Y6Z7Z8X9 + (-0.00594236+0i) * Y2X3X6Z7Z8Y9 + (0.00594236+0i) * Y2Y3X6Z7Z8X9 + (0.00594236+0i) * X2X3X7X8 + (0.00594236+0i) * X2Y3Y7X8 + (0.00594236+0i) * Y2X3X7Y8 + (0.00594236+0i) * Y2Y3Y7Y8 + (-0.0418473+0i) * X2X3Y8Y9 + (0.0418473+0i) * X2Y3Y8X9 + (0.0418473+0i) * Y2X3X8Y9 + (-0.0418473+0i) * Y2Y3X8X9 + (-0.00421737+0i) * X2X3Y10Y11 + (0.00421737+0i) * X2Y3Y10X11 + (0.00421737+0i) * Y2X3X10Y11 + (-0.00421737+0i) * Y2Y3X10X11 + (-0.00012261+0i) * X2Z3X4X6Z7Z8Z9X10 + (0.00142622+0i) * X2Z3X4Y6Z7Z8Z9Y10 + (-0.00154883+0i) * X2Z3Y4Y6Z7Z8Z9X10 + (-0.00154883+0i) * Y2Z3X4X6Z7Z8Z9Y10 + (0.00142622+0i) * Y2Z3Y4X6Z7Z8Z9X10 + (-0.00012261+0i) * Y2Z3Y4Y6Z7Z8Z9Y10 + (0.000779184+0i) * X2Z3X4X8Z9X10 + (-0.00906361+0i) * X2Z3X4Y8Z9Y10 + (0.00984279+0i) * X2Z3Y4Y8Z9X10 + (0.00984279+0i) * Y2Z3X4X8Z9Y10 + (-0.00906361+0i) * Y2Z3Y4X8Z9X10 + (0.000779184+0i) * Y2Z3Y4Y8Z9Y10 + (0.0012334+0i) * X2Z3Z4X5Y6Z7Z8Z9Z10Y11 + (-0.0012334+0i) * X2Z3Z4Y5Y6Z7Z8Z9Z10X11 + (-0.0012334+0i) * Y2Z3Z4X5X6Z7Z8Z9Z10Y11 + (0.0012334+0i) * Y2Z3Z4Y5X6Z7Z8Z9Z10X11 + (-0.00031543+0i) * X2Z3Z4X5X7Z8Z9X10 + (-0.00031543+0i) * X2Z3Z4Y5Y7Z8Z9X10 + (-0.00031543+0i) * Y2Z3Z4X5X7Z8Z9Y10 + (-0.00031543+0i) * Y2Z3Z4Y5Y7Z8Z9Y10 + (-0.00783823+0i) * X2Z3Z4X5Y8Z9Z10Y11 + (0.00783823+0i) * X2Z3Z4Y5Y8Z9Z10X11 + (0.00783823+0i) * Y2Z3Z4X5X8Z9Z10Y11 + (-0.00783823+0i) * Y2Z3Z4Y5X8Z9Z10X11 + (0.00200456+0i) * X2Z3Z4X5X9X10 + (0.00200456+0i) * X2Z3Z4Y5Y9X10 + (0.00200456+0i) * Y2Z3Z4X5X9Y10 + (0.00200456+0i) * Y2Z3Z4Y5Y9Y10 + (-0.00442998+0i) * Z2X6Z7X8 + (-0.00442998+0i) * Z2Y6Z7Y8 + (0.00151238+0i) * Z2X7Z8X9 + (0.00151238+0i) * Z2Y7Z8Y9 + (0.00019282+0i) * X2Z3X4X7Z8Z9Z10X11 + (0.00019282+0i) * X2Z3X4Y7Z8Z9Z10Y11 + (0.00019282+0i) * Y2Z3Y4X7Z8Z9Z10X11 + (0.00019282+0i) * Y2Z3Y4Y7Z8Z9Z10Y11 + (-0.00122537+0i) * X2Z3X4X9Z10X11 + (-0.00122537+0i) * X2Z3X4Y9Z10Y11 + (-0.00122537+0i) * Y2Z3Y4X9Z10X11 + (-0.00122537+0i) * Y2Z3Y4Y9Z10Y11 + (-0.00031543+0i) * X3X4X6Z7Z8Z9Z10X11 + (-0.00031543+0i) * X3Y4Y6Z7Z8Z9Z10X11 + (-0.00031543+0i) * Y3X4X6Z7Z8Z9Z10Y11 + (-0.00031543+0i) * Y3Y4Y6Z7Z8Z9Z10Y11 + (0.0012334+0i) * X3X4Y7Z8Z9Y10 + (-0.0012334+0i) * X3Y4Y7Z8Z9X10 + (-0.0012334+0i) * Y3X4X7Z8Z9Y10 + (0.0012334+0i) * Y3Y4X7Z8Z9X10 + (0.00200456+0i) * X3X4X8Z9Z10X11 + (0.00200456+0i) * X3Y4Y8Z9Z10X11 + (0.00200456+0i) * Y3X4X8Z9Z10Y11 + (0.00200456+0i) * Y3Y4Y8Z9Z10Y11 + (-0.00783823+0i) * X3X4Y9Y10 + (0.00783823+0i) * X3Y4Y9X10 + (0.00783823+0i) * Y3X4X9Y10 + (-0.00783823+0i) * Y3Y4X9X10 + (-0.00012261+0i) * X3Z4X5X7Z8Z9Z10X11 + (0.00142622+0i) * X3Z4X5Y7Z8Z9Z10Y11 + (-0.00154883+0i) * X3Z4Y5Y7Z8Z9Z10X11 + (-0.00154883+0i) * Y3Z4X5X7Z8Z9Z10Y11 + (0.00142622+0i) * Y3Z4Y5X7Z8Z9Z10X11 + (-0.00012261+0i) * Y3Z4Y5Y7Z8Z9Z10Y11 + (0.000779184+0i) * X3Z4X5X9Z10X11 + (-0.00906361+0i) * X3Z4X5Y9Z10Y11 + (0.00984279+0i) * X3Z4Y5Y9Z10X11 + (0.00984279+0i) * Y3Z4X5X9Z10Y11 + (-0.00906361+0i) * Y3Z4Y5X9Z10X11 + (0.000779184+0i) * Y3Z4Y5Y9Z10Y11 + (0.00151238+0i) * Z3X6Z7X8 + (0.00151238+0i) * Z3Y6Z7Y8 + (0.00019282+0i) * X3Z4X5X6Z7Z8Z9X10 + (0.00019282+0i) * X3Z4X5Y6Z7Z8Z9Y10 + (0.00019282+0i) * Y3Z4Y5X6Z7Z8Z9X10 + (0.00019282+0i) * Y3Z4Y5Y6Z7Z8Z9Y10 + (-0.00442998+0i) * Z3X7Z8X9 + (-0.00442998+0i) * Z3Y7Z8Y9 + (-0.00122537+0i) * X3Z4X5X8Z9X10 + (-0.00122537+0i) * X3Z4X5Y8Z9Y10 + (-0.00122537+0i) * Y3Z4Y5X8Z9X10 + (-0.00122537+0i) * Y3Z4Y5Y8Z9Y10 + (-0.00878244+0i) * X4X5Y6Y7 + (0.00878244+0i) * X4Y5Y6X7 + (0.00878244+0i) * Y4X5X6Y7 + (-0.00878244+0i) * Y4Y5X6X7 + (-0.00878244+0i) * X4X5Y8Y9 + (0.00878244+0i) * X4Y5Y8X9 + (0.00878244+0i) * Y4X5X8Y9 + (-0.00878244+0i) * Y4Y5X8X9 + (-0.00725868+0i) * X4X5Y10Y11 + (0.00725868+0i) * X4Y5Y10X11 + (0.00725868+0i) * Y4X5X10Y11 + (-0.00725868+0i) * Y4Y5X10X11 + (-0.00496705+0i) * X6X7Y8Y9 + (0.00496705+0i) * X6Y7Y8X9 + (0.00496705+0i) * Y6X7X8Y9 + (-0.00496705+0i) * Y6Y7X8X9 + (-0.00520528+0i) * X6X7Y10Y11 + (0.00520528+0i) * X6Y7Y10X11 + (0.00520528+0i) * Y6X7X10Y11 + (-0.00520528+0i) * Y6Y7X10X11 + (-0.00520528+0i) * X8X9Y10Y11 + (0.00520528+0i) * X8Y9Y10X11 + (0.00520528+0i) * Y8X9X10Y11 + (-0.00520528+0i) * Y8Y9X10X11\n",
      "[Cudaq] Number of terms in the Qubit hamiltonian:  383\n"
     ]
    }
   ],
   "source": [
    "geomerty = 'N 0.0 0.0 0.56; N 0.0 0.0 -0.56'\n",
    "molecular_data = get_mol_hamiltonian(xyz= geomerty, spin=1, charge=1, basis='631g', nele_cas=5, norb_cas=6, ccsd=True, casci = True, verbose=True)\n",
    "\n",
    "obi = molecular_data[0]\n",
    "tbi = molecular_data[1]\n",
    "ecore = molecular_data[2]\n",
    "nelectrons = molecular_data[3]\n",
    "norbitals = molecular_data[4]\n",
    "fermionic_hamiltonian = molecular_data[5]\n",
    "energies = molecular_data[6]\n",
    "\n",
    "print('-------------------------------')\n",
    "print('Molecular data')\n",
    "print('-------------------------------')\n",
    "print('Number of electrons in the active space: ', nelectrons)\n",
    "print('Number of spatial orbitals in the active space: ', norbitals)\n",
    "print('Number of spin molecular orbitals: ', 2 * norbitals)\n",
    "print('Core energy: ', ecore)\n",
    "\n",
    "# Compute the qubit hamiltonian.\n",
    "spin_ham = jordan_wigner_fermion(obi, tbi, ecore)\n",
    "print('[Cudaq] Qubit hamiltonian: ', spin_ham)\n",
    "print('[Cudaq] Number of terms in the Qubit hamiltonian: ', spin_ham.term_count)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7b58a2c",
   "metadata": {},
   "source": [
    "### (e) Generate electronic Hamiltonian using UHF\n",
    "\n",
    "Note: in case of UHF only electronic Hamiltonian from HF molecular orbital are supported. Calculating the electron integrals from natural orbitals or CASSCF orbitals is not supported yet."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "edd137f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "overwrite output file: N 0-pyscf.log\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARN: system HOMO -0.376086973991374 >= system LUMO -0.376086973991374\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[pyscf] Total number of alpha molecular orbitals =  18\n",
      "[pyscf] Total number of beta molecular orbitals =  18\n",
      "[pyscf] Total number of electrons =  13\n",
      "[pyscf] HF energy =  -108.28933888377188\n",
      "[pyscf] Converting nele_cas from 5 to (3, 2) (alpha, beta)\n",
      "[pyscf] UR-CASCI energy using molecular orbitals=  -108.38773041848827\n",
      "[pyscf] Converting nele_cas from 5 to (3, 2) (alpha, beta)\n",
      "[pyscf] UR-CCSD energy of the active space using molecular orbitals=  -108.38620486132402\n",
      "[pyscf] Converting nele_cas from 5 to (3, 2) (alpha, beta)\n",
      "-------------------------------\n",
      "Molecular data\n",
      "-------------------------------\n",
      "Number of electrons in the active space:  5\n",
      "Number of spatial orbitals in the active space:  6\n",
      "Number of spin molecular orbitals:  12\n",
      "Core energy:  -97.75004616632093\n",
      "{'hf': -108.28933888377188, 'mp2': None, 'casci': -108.38773041848827, 'casscf': None, 'ccsd': -108.38620486132402, 'fci': None, 'nuclear_repulsion': 23.15150297775}\n",
      "[Cudaq] Qubit hamiltonian:  (-106.641+0i) + (0.251399+0i) * Z0 + (0.266554+0i) * Z1 + (0.265567+0i) * Z2 + (0.266554+0i) * Z3 + (0.265567+0i) * Z4 + (0.232584+0i) * Z5 + (0.0637266+0i) * Z6 + (0.0648997+0i) * Z7 + (0.0637266+0i) * Z8 + (0.0648997+0i) * Z9 + (-0.19849+0i) * Z10 + (-0.195548+0i) * Z11 + (0.130154+0i) * Z0Z1 + (0.123285+0i) * Z0Z2 + (0.130154+0i) * Z0Z3 + (0.123285+0i) * Z0Z4 + (0.141709+0i) * Z0Z5 + (0.118922+0i) * Z0Z6 + (0.127641+0i) * Z0Z7 + (0.118922+0i) * Z0Z8 + (0.127641+0i) * Z0Z9 + (0.0941675+0i) * Z0Z10 + (0.0981197+0i) * Z0Z11 + (0.135794+0i) * Z1Z2 + (0.126993+0i) * Z1Z3 + (0.141271+0i) * Z1Z4 + (0.118702+0i) * Z1Z5 + (0.127751+0i) * Z1Z6 + (0.099962+0i) * Z1Z7 + (0.132746+0i) * Z1Z8 + (0.113024+0i) * Z1Z9 + (0.0967903+0i) * Z1Z10 + (0.08889+0i) * Z1Z11 + (0.141271+0i) * Z2Z3 + (0.127861+0i) * Z2Z4 + (0.125754+0i) * Z2Z5 + (0.0926024+0i) * Z2Z6 + (0.132863+0i) * Z2Z7 + (0.121638+0i) * Z2Z8 + (0.127472+0i) * Z2Z9 + (0.0923836+0i) * Z2Z10 + (0.0928646+0i) * Z2Z11 + (0.135794+0i) * Z3Z4 + (0.118702+0i) * Z3Z5 + (0.132746+0i) * Z3Z6 + (0.113024+0i) * Z3Z7 + (0.127751+0i) * Z3Z8 + (0.099962+0i) * Z3Z9 + (0.0967903+0i) * Z3Z10 + (0.08889+0i) * Z3Z11 + (0.125754+0i) * Z4Z5 + (0.121638+0i) * Z4Z6 + (0.127472+0i) * Z4Z7 + (0.0926024+0i) * Z4Z8 + (0.132863+0i) * Z4Z9 + (0.0923836+0i) * Z4Z10 + (0.0928646+0i) * Z4Z11 + (0.123223+0i) * Z5Z6 + (0.114379+0i) * Z5Z7 + (0.123223+0i) * Z5Z8 + (0.114379+0i) * Z5Z9 + (0.0993155+0i) * Z5Z10 + (0.0866001+0i) * Z5Z11 + (0.128223+0i) * Z6Z7 + (0.116067+0i) * Z6Z8 + (0.122917+0i) * Z6Z9 + (0.0892601+0i) * Z6Z10 + (0.0915793+0i) * Z6Z11 + (0.122917+0i) * Z7Z8 + (0.115228+0i) * Z7Z9 + (0.094734+0i) * Z7Z10 + (0.0866918+0i) * Z7Z11 + (0.128223+0i) * Z8Z9 + (0.0892601+0i) * Z8Z10 + (0.0915793+0i) * Z8Z11 + (0.094734+0i) * Z9Z10 + (0.0866918+0i) * Z9Z11 + (0.0904142+0i) * Z10Z11 + (0.00349312+0i) * X0X1Y2Z3Z4Y5 + (-0.00349312+0i) * X0Y1Y2Z3Z4X5 + (-0.00349312+0i) * Y0X1X2Z3Z4Y5 + (0.00349312+0i) * Y0Y1X2Z3Z4X5 + (0.0059957+0i) * X0X1Y4Y5 + (-0.0059957+0i) * X0Y1Y4X5 + (-0.0059957+0i) * Y0X1X4Y5 + (0.0059957+0i) * Y0Y1X4X5 + (0.000974687+0i) * X0X1Y6Z7Z8Z9Z10Y11 + (-0.000974687+0i) * X0Y1Y6Z7Z8Z9Z10X11 + (-0.000974687+0i) * Y0X1X6Z7Z8Z9Z10Y11 + (0.000974687+0i) * Y0Y1X6Z7Z8Z9Z10X11 + (-0.00573123+0i) * X0X1X7Z8Z9X10 + (-0.00573123+0i) * X0Y1Y7Z8Z9X10 + (-0.00573123+0i) * Y0X1X7Z8Z9Y10 + (-0.00573123+0i) * Y0Y1Y7Z8Z9Y10 + (0.00170474+0i) * X0X1Y8Z9Z10Y11 + (-0.00170474+0i) * X0Y1Y8Z9Z10X11 + (-0.00170474+0i) * Y0X1X8Z9Z10Y11 + (0.00170474+0i) * Y0Y1X8Z9Z10X11 + (-0.00349845+0i) * X0X1X9X10 + (-0.00349845+0i) * X0Y1Y9X10 + (-0.00349845+0i) * Y0X1X9Y10 + (-0.00349845+0i) * Y0Y1Y9Y10 + (-0.00857072+0i) * X0Z1X2X6Z7Z8Z9X10 + (0.000664835+0i) * X0Z1X2Y6Z7Z8Z9Y10 + (-0.00923556+0i) * X0Z1Y2Y6Z7Z8Z9X10 + (-0.00923556+0i) * Y0Z1X2X6Z7Z8Z9Y10 + (0.000664835+0i) * Y0Z1Y2X6Z7Z8Z9X10 + (-0.00857072+0i) * Y0Z1Y2Y6Z7Z8Z9Y10 + (-6.97775e-05+0i) * X0Z1X2X8Z9X10 + (5.41268e-06+0i) * X0Z1X2Y8Z9Y10 + (-7.51902e-05+0i) * X0Z1Y2Y8Z9X10 + (-7.51902e-05+0i) * Y0Z1X2X8Z9Y10 + (5.41268e-06+0i) * Y0Z1Y2X8Z9X10 + (-6.97775e-05+0i) * Y0Z1Y2Y8Z9Y10 + (-0.0059957+0i) * X0Z1X2X3Z4X5 + (-0.0059957+0i) * X0Z1X2Y3Z4Y5 + (-0.0059957+0i) * Y0Z1Y2X3Z4X5 + (-0.0059957+0i) * Y0Z1Y2Y3Z4Y5 + (-0.00349312+0i) * X0Z1Z2X3Y4Y5 + (0.00349312+0i) * X0Z1Z2Y3Y4X5 + (0.00349312+0i) * Y0Z1Z2X3X4Y5 + (-0.00349312+0i) * Y0Z1Z2Y3X4X5 + (0.00170474+0i) * X0Z1Z2X3Y6Z7Z8Z9Z10Y11 + (-0.00170474+0i) * X0Z1Z2Y3Y6Z7Z8Z9Z10X11 + (-0.00170474+0i) * Y0Z1Z2X3X6Z7Z8Z9Z10Y11 + (0.00170474+0i) * Y0Z1Z2Y3X6Z7Z8Z9Z10X11 + (-0.00349845+0i) * X0Z1Z2X3X7Z8Z9X10 + (-0.00349845+0i) * X0Z1Z2Y3Y7Z8Z9X10 + (-0.00349845+0i) * Y0Z1Z2X3X7Z8Z9Y10 + (-0.00349845+0i) * Y0Z1Z2Y3Y7Z8Z9Y10 + (-0.000974687+0i) * X0Z1Z2X3Y8Z9Z10Y11 + (0.000974687+0i) * X0Z1Z2Y3Y8Z9Z10X11 + (0.000974687+0i) * Y0Z1Z2X3X8Z9Z10Y11 + (-0.000974687+0i) * Y0Z1Z2Y3X8Z9Z10X11 + (0.00573123+0i) * X0Z1Z2X3X9X10 + (0.00573123+0i) * X0Z1Z2Y3Y9X10 + (0.00573123+0i) * Y0Z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Z4Y5Y8X9 + (-0.00767363+0i) * Y0Z1Z2Z3Z4X5X8Y9 + (0.00767363+0i) * Y0Z1Z2Z3Z4Y5X8X9 + (0.00816074+0i) * X0Z1Z2Z3Z4X5Y10Y11 + (-0.00816074+0i) * X0Z1Z2Z3Z4Y5Y10X11 + (-0.00816074+0i) * Y0Z1Z2Z3Z4X5X10Y11 + (0.00816074+0i) * Y0Z1Z2Z3Z4Y5X10X11 + (-0.00111981+0i) * X0Z1X2X7Z8Z9Z10X11 + (-0.00111981+0i) * X0Z1X2Y7Z8Z9Z10Y11 + (-0.00111981+0i) * Y0Z1Y2X7Z8Z9Z10X11 + (-0.00111981+0i) * Y0Z1Y2Y7Z8Z9Z10Y11 + (-0.000604822+0i) * X0Z1Z2Z3X4X7Z8Z9Z10X11 + (-0.000604822+0i) * X0Z1Z2Z3X4Y7Z8Z9Z10Y11 + (-0.000604822+0i) * Y0Z1Z2Z3Y4X7Z8Z9Z10X11 + (-0.000604822+0i) * Y0Z1Z2Z3Y4Y7Z8Z9Z10Y11 + (0.000604822+0i) * X0Z1X2X9Z10X11 + (0.000604822+0i) * X0Z1X2Y9Z10Y11 + (0.000604822+0i) * Y0Z1Y2X9Z10X11 + (0.000604822+0i) * Y0Z1Y2Y9Z10Y11 + (-0.00111981+0i) * X0Z1Z2Z3X4X9Z10X11 + (-0.00111981+0i) * X0Z1Z2Z3X4Y9Z10Y11 + (-0.00111981+0i) * Y0Z1Z2Z3Y4X9Z10X11 + (-0.00111981+0i) * Y0Z1Z2Z3Y4Y9Z10Y11 + (-0.0048313+0i) * Z1X2Z3X4 + (-0.0048313+0i) * Z1Y2Z3Y4 + (-0.0048313+0i) * X1X3 + (-0.0048313+0i) * Y1Y3 + (-0.00273892+0i) * X1X2Y3Y4 + (0.00273892+0i) * X1Y2Y3X4 + (0.00273892+0i) * Y1X2X3Y4 + (-0.00273892+0i) * Y1Y2X3X4 + (-0.0330849+0i) * X1X2X6X7 + (-0.0330849+0i) * X1Y2Y6X7 + (-0.0330849+0i) * Y1X2X6Y7 + (-0.0330849+0i) * Y1Y2Y6Y7 + (-0.0160578+0i) * X1X2X6Z7Z8X9 + (-0.0160578+0i) * X1Y2Y6Z7Z8X9 + (-0.0160578+0i) * Y1X2X6Z7Z8Y9 + (-0.0160578+0i) * Y1Y2Y6Z7Z8Y9 + (-0.0043462+0i) * X1X2Y7Y8 + (0.0043462+0i) * X1Y2Y7X8 + (0.0043462+0i) * Y1X2X7Y8 + (-0.0043462+0i) * Y1Y2X7X8 + (-0.000262503+0i) * X1X2X8X9 + (-0.000262503+0i) * X1Y2Y8X9 + (-0.000262503+0i) * Y1X2X8Y9 + (-0.000262503+0i) * Y1Y2Y8Y9 + (-0.00210013+0i) * X1X2X10X11 + (-0.00210013+0i) * X1Y2Y10X11 + (-0.00210013+0i) * Y1X2X10Y11 + (-0.00210013+0i) * Y1Y2Y10Y11 + (-0.00860195+0i) * X1Z2X3X7Z8X9 + (0.0216635+0i) * X1Z2X3Y7Z8Y9 + (-0.0302655+0i) * X1Z2Y3Y7Z8X9 + (-0.0302655+0i) * Y1Z2X3X7Z8Y9 + (0.0216635+0i) * Y1Z2Y3X7Z8X9 + (-0.00860195+0i) * Y1Z2Y3Y7Z8Y9 + (0.0048313+0i) * X1Z2X3Z4 + (0.0048313+0i) * Y1Z2Y3Z4 + (-0.00380909+0i) * X1Z2Z3X4X6X7 + (-0.00380909+0i) * X1Z2Z3Y4Y6X7 + (-0.00380909+0i) * Y1Z2Z3X4X6Y7 + (-0.00380909+0i) * Y1Z2Z3Y4Y6Y7 + (6.53579e-05+0i) * X1Z2Z3X4X6Z7Z8X9 + (6.53579e-05+0i) * X1Z2Z3Y4Y6Z7Z8X9 + (6.53579e-05+0i) * Y1Z2Z3X4X6Z7Z8Y9 + (6.53579e-05+0i) * Y1Z2Z3Y4Y6Z7Z8Y9 + (-0.0328877+0i) * X1Z2Z3X4Y7Y8 + (0.0328877+0i) * X1Z2Z3Y4Y7X8 + (0.0328877+0i) * Y1Z2Z3X4X7Y8 + (-0.0328877+0i) * Y1Z2Z3Y4X7X8 + (-0.0242131+0i) * X1Z2Z3X4X8X9 + (-0.0242131+0i) * X1Z2Z3Y4Y8X9 + (-0.0242131+0i) * Y1Z2Z3X4X8Y9 + (-0.0242131+0i) * Y1Z2Z3Y4Y8Y9 + (-0.00360472+0i) * X1Z2Z3X4X10X11 + (-0.00360472+0i) * X1Z2Z3Y4Y10X11 + (-0.00360472+0i) * Y1Z2Z3X4X10Y11 + (-0.00360472+0i) * Y1Z2Z3Y4Y10Y11 + (-0.000668289+0i) * X1Z2Z3Z4X5X7Z8Z9Z10X11 + (0.00924165+0i) * X1Z2Z3Z4X5Y7Z8Z9Z10Y11 + (-0.00990994+0i) * X1Z2Z3Z4Y5Y7Z8Z9Z10X11 + (-0.00990994+0i) * Y1Z2Z3Z4X5X7Z8Z9Z10Y11 + (0.00924165+0i) * Y1Z2Z3Z4Y5X7Z8Z9Z10X11 + (-0.000668289+0i) * Y1Z2Z3Z4Y5Y7Z8Z9Z10Y11 + (-0.000407936+0i) * X1Z2Z3Z4X5X9Z10X11 + (0.00564128+0i) * X1Z2Z3Z4X5Y9Z10Y11 + (-0.00604921+0i) * X1Z2Z3Z4Y5Y9Z10X11 + (-0.00604921+0i) * Y1Z2Z3Z4X5X9Z10Y11 + (0.00564128+0i) * Y1Z2Z3Z4Y5X9Z10X11 + (-0.000407936+0i) * Y1Z2Z3Z4Y5Y9Z10Y11 + (-0.00424354+0i) * Z1X6Z7X8 + (-0.00424354+0i) * Z1Y6Z7Y8 + (-0.00424354+0i) * X1Z2X3Z6 + (-0.00424354+0i) * Y1Z2Y3Z6 + (0.00249788+0i) * X1Z2X3X6Z7X8 + (0.00249788+0i) * X1Z2X3Y6Z7Y8 + (0.00249788+0i) * Y1Z2Y3X6Z7X8 + (0.00249788+0i) * Y1Z2Y3Y6Z7Y8 + (0.000629689+0i) * X1Z2Z3Z4X5X6Z7Z8Z9X10 + (0.000629689+0i) * X1Z2Z3Z4X5Y6Z7Z8Z9Y10 + (0.000629689+0i) * Y1Z2Z3Z4Y5X6Z7Z8Z9X10 + (0.000629689+0i) * Y1Z2Z3Z4Y5Y6Z7Z8Z9Y10 + (0.0127083+0i) * Z1X7Z8X9 + (0.0127083+0i) * Z1Y7Z8Y9 + (0.0127083+0i) * X1Z2X3Z7 + (0.0127083+0i) * Y1Z2Y3Z7 + (0.00424354+0i) * X1Z2X3Z8 + (0.00424354+0i) * Y1Z2Y3Z8 + (0.00110133+0i) * X1Z2Z3Z4X5X8Z9X10 + (0.00110133+0i) * X1Z2Z3Z4X5Y8Z9Y10 + (0.00110133+0i) * Y1Z2Z3Z4Y5X8Z9X10 + (0.00110133+0i) * Y1Z2Z3Z4Y5Y8Z9Y10 + (-0.0127083+0i) * X1Z2X3Z9 + (-0.0127083+0i) * Y1Z2Y3Z9 + (0.0048313+0i) * X2X4 + (0.0048313+0i) * Y2Y4 + (-0.0242131+0i) * X2X3Y6Y7 + (0.0242131+0i) * X2Y3Y6X7 + (0.0242131+0i) * Y2X3X6Y7 + (-0.0242131+0i) * Y2Y3X6X7 + (0.0328877+0i) * X2X3Y6Z7Z8Y9 + (-0.0328877+0i) * X2Y3Y6Z7Z8X9 + (-0.0328877+0i) * Y2X3X6Z7Z8Y9 + (0.0328877+0i) * Y2Y3X6Z7Z8X9 + (-6.53579e-05+0i) * X2X3X7X8 + (-6.53579e-05+0i) * X2Y3Y7X8 + (-6.53579e-05+0i) * Y2X3X7Y8 + (-6.53579e-05+0i) * Y2Y3Y7Y8 + (-0.00380909+0i) * X2X3Y8Y9 + (0.00380909+0i) * X2Y3Y8X9 + (0.00380909+0i) * Y2X3X8Y9 + (-0.00380909+0i) * Y2Y3X8X9 + (-0.00360472+0i) * X2X3Y10Y11 + (0.00360472+0i) * X2Y3Y10X11 + (0.00360472+0i) * Y2X3X10Y11 + (-0.00360472+0i) * Y2Y3X10X11 + (0.000840433+0i) * X2Z3X4X6Z7X8 + (-0.0298765+0i) * X2Z3X4Y6Z7Y8 + (0.0307169+0i) * X2Z3Y4Y6Z7X8 + (0.0307169+0i) * Y2Z3X4X6Z7Y8 + (-0.0298765+0i) * Y2Z3Y4X6Z7X8 + (0.000840433+0i) * Y2Z3Y4Y6Z7Y8 + (0.0100946+0i) * X2Z3Z4X5Y6Z7Z8Z9Z10Y11 + (-0.0100946+0i) * X2Z3Z4Y5Y6Z7Z8Z9Z10X11 + (-0.0100946+0i) * Y2Z3Z4X5X6Z7Z8Z9Z10Y11 + (0.0100946+0i) * Y2Z3Z4Y5X6Z7Z8Z9Z10X11 + (-0.00179878+0i) * X2Z3Z4X5X7Z8Z9X10 + (-0.00179878+0i) * X2Z3Z4Y5Y7Z8Z9X10 + (-0.00179878+0i) * Y2Z3Z4X5X7Z8Z9Y10 + (-0.00179878+0i) * Y2Z3Z4Y5Y7Z8Z9Y10 + (8.21841e-05+0i) * X2Z3Z4X5Y8Z9Z10Y11 + (-8.21841e-05+0i) * X2Z3Z4Y5Y8Z9Z10X11 + (-8.21841e-05+0i) * Y2Z3Z4X5X8Z9Z10Y11 + (8.21841e-05+0i) * Y2Z3Z4Y5X8Z9Z10X11 + (0.000971543+0i) * X2Z3Z4X5X9X10 + (0.000971543+0i) * X2Z3Z4Y5Y9X10 + (0.000971543+0i) * Y2Z3Z4X5X9Y10 + (0.000971543+0i) * Y2Z3Z4Y5Y9Y10 + (0.000236409+0i) * Z2X6Z7X8 + (0.000236409+0i) * Z2Y6Z7Y8 + (-0.000236409+0i) * X2Z3X4Z6 + (-0.000236409+0i) * Y2Z3Y4Z6 + (0.00411158+0i) * Z2X7Z8X9 + (0.00411158+0i) * Z2Y7Z8Y9 + (-0.00411158+0i) * X2Z3X4Z7 + (-0.00411158+0i) * Y2Z3Y4Z7 + (0.00269587+0i) * X2Z3X4X7Z8X9 + (0.00269587+0i) * X2Z3X4Y7Z8Y9 + (0.00269587+0i) * Y2Z3Y4X7Z8X9 + (0.00269587+0i) * Y2Z3Y4Y7Z8Y9 + (0.000236409+0i) * X2Z3X4Z8 + (0.000236409+0i) * Y2Z3Y4Z8 + (0.00411158+0i) * X2Z3X4Z9 + (0.00411158+0i) * Y2Z3Y4Z9 + (0.000262503+0i) * X3X4X6X7 + (0.000262503+0i) * X3Y4Y6X7 + (0.000262503+0i) * Y3X4X6Y7 + (0.000262503+0i) * Y3Y4Y6Y7 + (-0.0043462+0i) * X3X4X6Z7Z8X9 + (-0.0043462+0i) * X3Y4Y6Z7Z8X9 + (-0.0043462+0i) * Y3X4X6Z7Z8Y9 + (-0.0043462+0i) * Y3Y4Y6Z7Z8Y9 + (-0.0160578+0i) * X3X4Y7Y8 + (0.0160578+0i) * X3Y4Y7X8 + (0.0160578+0i) * Y3X4X7Y8 + (-0.0160578+0i) * Y3Y4X7X8 + (0.0330849+0i) * X3X4X8X9 + (0.0330849+0i) * X3Y4Y8X9 + (0.0330849+0i) * Y3X4X8Y9 + (0.0330849+0i) * Y3Y4Y8Y9 + (0.00210013+0i) * X3X4X10X11 + (0.00210013+0i) * X3Y4Y10X11 + (0.00210013+0i) * Y3X4X10Y11 + (0.00210013+0i) * Y3Y4Y10Y11 + (-0.000407936+0i) * X3Z4X5X7Z8Z9Z10X11 + (0.00564128+0i) * X3Z4X5Y7Z8Z9Z10Y11 + (-0.00604921+0i) * X3Z4Y5Y7Z8Z9Z10X11 + (-0.00604921+0i) * Y3Z4X5X7Z8Z9Z10Y11 + (0.00564128+0i) * Y3Z4Y5X7Z8Z9Z10X11 + (-0.000407936+0i) * Y3Z4Y5Y7Z8Z9Z10Y11 + (0.000668289+0i) * X3Z4X5X9Z10X11 + (-0.00924165+0i) * X3Z4X5Y9Z10Y11 + (0.00990994+0i) * X3Z4Y5Y9Z10X11 + (0.00990994+0i) * Y3Z4X5X9Z10Y11 + (-0.00924165+0i) * Y3Z4Y5X9Z10X11 + (0.000668289+0i) * Y3Z4Y5Y9Z10Y11 + (0.00424354+0i) * Z3X6Z7X8 + (0.00424354+0i) * Z3Y6Z7Y8 + (0.00110133+0i) * X3Z4X5X6Z7Z8Z9X10 + (0.00110133+0i) * X3Z4X5Y6Z7Z8Z9Y10 + (0.00110133+0i) * Y3Z4Y5X6Z7Z8Z9X10 + (0.00110133+0i) * Y3Z4Y5Y6Z7Z8Z9Y10 + (-0.0127083+0i) * Z3X7Z8X9 + (-0.0127083+0i) * Z3Y7Z8Y9 + (-0.000629689+0i) * X3Z4X5X8Z9X10 + (-0.000629689+0i) * X3Z4X5Y8Z9Y10 + (-0.000629689+0i) * Y3Z4Y5X8Z9X10 + (-0.000629689+0i) * Y3Z4Y5Y8Z9Y10 + (-8.21841e-05+0i) * X4X5Y6Z7Z8Z9Z10Y11 + (8.21841e-05+0i) * X4Y5Y6Z7Z8Z9Z10X11 + (8.21841e-05+0i) * Y4X5X6Z7Z8Z9Z10Y11 + (-8.21841e-05+0i) * Y4Y5X6Z7Z8Z9Z10X11 + (-0.000971543+0i) * X4X5X7Z8Z9X10 + (-0.000971543+0i) * X4Y5Y7Z8Z9X10 + (-0.000971543+0i) * Y4X5X7Z8Z9Y10 + (-0.000971543+0i) * Y4Y5Y7Z8Z9Y10 + (0.0100946+0i) * X4X5Y8Z9Z10Y11 + (-0.0100946+0i) * X4Y5Y8Z9Z10X11 + (-0.0100946+0i) * Y4X5X8Z9Z10Y11 + (0.0100946+0i) * Y4Y5X8Z9Z10X11 + (-0.00179878+0i) * X4X5X9X10 + (-0.00179878+0i) * X4Y5Y9X10 + (-0.00179878+0i) * Y4X5X9Y10 + (-0.00179878+0i) * Y4Y5Y9Y10 + (-0.000236409+0i) * Z4X6Z7X8 + (-0.000236409+0i) * Z4Y6Z7Y8 + (-0.00411158+0i) * Z4X7Z8X9 + (-0.00411158+0i) * Z4Y7Z8Y9 + (0.0041934+0i) * Z6X7Z8X9 + (0.0041934+0i) * Z6Y7Z8Y9 + (-0.0041934+0i) * X6X8 + (-0.0041934+0i) * Y6Y8 + (-0.0026529+0i) * X6X7Y8Y9 + (0.0026529+0i) * X6Y7Y8X9 + (0.0026529+0i) * Y6X7X8Y9 + (-0.0026529+0i) * Y6Y7X8X9 + (-0.00450764+0i) * X6X7Y10Y11 + (0.00450764+0i) * X6Y7Y10X11 + (0.00450764+0i) * Y6X7X10Y11 + (-0.00450764+0i) * Y6Y7X10X11 + (0.0041934+0i) * X6Z7X8Z9 + (0.0041934+0i) * Y6Z7Y8Z9 + (0.00248224+0i) * X6Z7Z8X9Y10Y11 + (-0.00248224+0i) * X6Z7Z8Y9Y10X11 + (-0.00248224+0i) * Y6Z7Z8X9X10Y11 + (0.00248224+0i) * Y6Z7Z8Y9X10X11 + (-0.0041934+0i) * X7X9 + (-0.0041934+0i) * Y7Y9 + (-0.00248224+0i) * X7X8X10X11 + (-0.00248224+0i) * X7Y8Y10X11 + (-0.00248224+0i) * Y7X8X10Y11 + (-0.00248224+0i) * Y7Y8Y10Y11 + (-0.00450764+0i) * X8X9Y10Y11 + (0.00450764+0i) * X8Y9Y10X11 + (0.00450764+0i) * Y8X9X10Y11 + (-0.00450764+0i) * Y8Y9X10X11\n",
      "[Cudaq] Number of terms in the Qubit hamiltonian:  431\n"
     ]
    }
   ],
   "source": [
    "geomerty = 'N 0.0 0.0 0.56; N 0.0 0.0 -0.56'\n",
    "molecular_data = get_mol_hamiltonian(xyz= geomerty, spin=1, charge=1, basis='631g', UR=True, nele_cas=5, norb_cas=6, ccsd=True, casci = True, verbose=True)\n",
    "\n",
    "obi = molecular_data[0]\n",
    "tbi = molecular_data[1]\n",
    "ecore = molecular_data[2]\n",
    "nelectrons = molecular_data[3]\n",
    "norbitals = molecular_data[4]\n",
    "fermionic_hamiltonian = molecular_data[5]\n",
    "energies = molecular_data[6]\n",
    "\n",
    "print('-------------------------------')\n",
    "print('Molecular data')\n",
    "print('-------------------------------')\n",
    "print('Number of electrons in the active space: ', nelectrons)\n",
    "print('Number of spatial orbitals in the active space: ', norbitals)\n",
    "print('Number of spin molecular orbitals: ', 2 * norbitals)\n",
    "print('Core energy: ', ecore)\n",
    "print(energies)\n",
    "\n",
    "# Compute the qubit hamiltonian.\n",
    "spin_ham = jordan_wigner_fermion(obi, tbi, ecore)\n",
    "print('[Cudaq] Qubit hamiltonian: ', spin_ham)\n",
    "print('[Cudaq] Number of terms in the Qubit hamiltonian: ', spin_ham.term_count)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
